我正在使用 iOS 7,我使用 NSURLSession 与服务器连接。我尝试了以下代码:
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration
defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:
defaultConfigObject delegate: nil delegateQueue:
[NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://**********/api/Users"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params
=@"FirstName=Aravind&LastName=India&WorkingEmailAddress=aravind@gmail.com";
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding:
NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
我得到的错误如下:
{ status code: 400, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 0;
Date = "Wed, 20 Nov 2013 05:05:39 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} } (null)
2013-11-20 10:35:39.723 SessionApp[27223:a0b] Data = .
我无法理解。我应该怎么做才能检查错误?请指导我,我是iOS新手。