0

我正在使用 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新手。

4

2 回答 2

1

It seems like you are unaware of how to send Parameters request to Server. I would like you to have a look at this library and its examples. It would help you get out of this problem.

https://github.com/AFNetworking/AFNetworking

Example for sending parameters

NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@"firstName", @"lastName", @"Email"]};
于 2013-11-20T05:27:05.800 回答
0

根据服务器响应,没有要发布的内容。

您可以通过此链接获得想法

于 2013-11-20T05:23:43.903 回答