Monday 18 November 2013

how to post json string to .net webservice through POST method in objective C (Xcode)

NSString *jsonStr=@"{\"email\":\"xxx@gmail.com\",\"pwd\":\"xxx\"}";
   
   
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
   
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
   
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"Your URL"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
   
   
    NSURLResponse *response;
   
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
   
    NSString *responseStr = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding: NSUTF8StringEncoding];
 

No comments:

Post a Comment