2012. 2. 11. 11:24

[objective c] POST 데이터 전송

- (void)connectToServer
{
    NSString *url = @"http://192.168.0.103/p/Login/CheckEmailDuplicate.php";
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
   
    NSString *post = [NSString stringWithFormat:@"email=동해물과백두산이마르고닳도록"];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
   
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"Mozilla/4.0 (compatible;)" forHTTPHeaderField:@"User-Agent"];
    [request setHTTPBody:postData];
   
    [NSURLConnection connectionWithRequest:request delegate:self];
}

// HTML이 처리되고 난 뒤 얻는 데이터
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];   
    NSLog(@"recieved : %@",returnString);
   
    //
    if ([returnString isEqualToString:@""]) {

    }
}

 
// post를 보낸 후 쿠키 수신
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
        NSHTTPCookie *cookie;
       
        for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])       
        {
            NSLog(@"%@",[cookie description]);       
        }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   
}
Trackback 0 Comment 0