Json response is successful in browser but displays error message in simulator

时间:2016-04-25 09:24:50

标签: ios objective-c json xcode nsurlconnection

I have a server which sends JSON web response using the browser. However when the same URL is passed through the simulator throws an error message. The code is as below:

NSString *postString = [NSString stringWithFormat:@"username=\"tango\"&password=\"charlie\""];

NSLog(@"%@",postString);
//encode post string in supported encoding format in post data
postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

if(connection)
  return YES;
else 
  return NO;

[NSMutableRequest setHTTPRequest:urlString];

The header part is as follows:

[request setURL:url];

//set HTTP method Post
[request setHTTPMethod:@"POST"];

//set HTTP header field with length of post data
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

//set encoding value for HTTP header field
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

 //set HTTP body or url request with postData
 [request setHTTPBody:postData];

I expected a successful log message but the simulator shows an invalid username and password log.

What could be the reason?

0 个答案:

没有答案