/*
아래 같은 방식으로 smsURL이라는 주소로 post 형식으로 데이터를 감싸 전송이 가능해진다.
*/
- (void)connectToServer
{
NSString *smsURL = @"http://www.google.co.kr";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSString *post = [NSString stringWithFormat:@"password=%@&id=%@",@"password",@"id"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[request setURL:[NSURL URLWithString:smsURL]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"Mozilla/4.0 (compatible;)" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self ];
}
/*
아래 Delegate에서는 HTML이 처리되고 난 후 받는 데이터를 얻을 수 있다.
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(returnString);
}
/*
아래 Delegate를 이용하면 post를 보낸 후 쿠키를 얻을 수 있다.
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
NSHTTPCookie *cookie;
int i=0;
for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
NSLog([cookie description]);
}
}
'영삼이의 IT정보' 카테고리의 다른 글
iOS5의 UDID 정책 변경에 대한 퀵 리뷰 (iOS5 beta 6 기준) (0) | 2012.05.31 |
---|---|
ASIHttpRequest를 ARC와 함께 쓰기 (XCode 4.2) (0) | 2012.05.30 |
PCRE 정규표현식 기본기 (0) | 2012.05.29 |
테스트 한번 해볼까 (0) | 2012.05.20 |
부트캠프 관련 메모 (0) | 2012.05.19 |