2011년 3월 29일 화요일

아이폰개발: https로 요청하기....

요청하기....

NSURL *url;

NSMutableURLRequest *request;

NSURLConnection *connection;

//NSString *httpBody;

//URL reques 할당.

url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", AUTHENTICATION_URL, phoneNumberFld.text]];

request = [[NSMutableURLRequest alloc] initWithURL: url];

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[connection release];

[request release];


델리게이트에선....

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

//NSLog(@"did receivedata [%@]byte",[NSString stringWithUTF8String:(const char*)[data bytes]]);

//if([data length] > 0)

[responseData appendData:data];

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

NSLog(@"didReceiveResponse");

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

NSLog(@"Headers = [%@]", [httpResponse allHeaderFields]);

[responseData setLength:0];

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

NSLog(@"connectionDidFinishLoading");

//JSON처리는 이쪽에서 행할것!

//NSString* recvString = [NSString stringWithUTF8String:(const char*)[responseData bytes]];

NSString *recvString = [[NSString alloc] initWithData:responseData

encoding:NSUTF8StringEncoding];


dispRecvData.text = [NSString stringWithFormat:@"", recvString];

[self checkJsonValue:dispRecvData.text];

}


- (void)connection:(NSURLConnection *)connection

didSendBodyData:(NSInteger)bytesWritten

totalBytesWritten:(NSInteger)totalBytesWritten

totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

{

NSLog(@"didSendBodyData");

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

NSLog(@"didFailWithError");

return;

}

// 나중에 필요하면 주석을 풀어 사용하세요.

//- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

// NSLog(@"canAuthenticateAgainstProtectionSpace");

// return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

//}


//인증처리가 필요해요!!!!

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

NSLog(@"didReceiveAuthenticationChallenge");

if ([challenge previousFailureCount] == 0) {

[[challenge sender] useCredential:[NSURLCredential credentialWithUser:KUSER password:KPASS persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];

} else {

[[challenge sender] cancelAuthenticationChallenge:challenge];

}

}

댓글 없음:

댓글 쓰기