'setValue'에 해당되는 글 1건

  1. 2012.05.29 iPhone NSURLRequest 사용법
영삼이의 IT정보2012. 5. 29. 15:14

/* 

아래 같은 방식으로 smsURL이라는 주소로 post 형식으로 데이터를 감싸 전송이 가능해진다.

*/

- (void)connectToServer

{

NSString *smsURL = @"http://www.google.co.kr";

NSMutableURLRequest *request = [[[NSMutableURLRequest allocinitautorelease];

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 allocinitWithData:data encoding:NSUTF8StringEncoding];

NSLog(returnString);

}


/* 

아래 Delegate를 이용하면 post를 보낸 후 쿠키를 얻을 수 있다.

*/

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

{

NSHTTPCookie *cookie;

int i=0;

for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStoragecookies])

{

NSLog([cookie description]);

}

}

Posted by 다오나무