NSURLSession POST无效,并在iphone

时间:2016-02-01 08:17:07

标签: ios web-services http-post nsurlsession

当我使用NSURLSession时,通过浏览器发布将结果返回为200状态但是当我通过IOS中的代码发送它时,我得到500状态代码,如下所示。

Response:<NSHTTPURLResponse: 0x14e754240> { URL: urlAPI } { status code: 500, headers {
    "Cache-Control" = private;
    "Content-Length" = 30;
    "Content-Type" = "text/plain; charset=utf-8";
    Date = "Thu, 28 Jan 2016 12:59:10 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} } 

以下是我的代码

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
   NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

   NSURL * url = [NSURL URLWithString:@"HERE IS MY URL"];
   NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
   NSString * params =@"MY PARAMETERS";
   [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];

此代码以前工作过,但它现在抛出错误(API代码也没有更改),我在哪里做错了。帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

尝试在NSDictionary中发送参数:

NSError *error;

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@“[Your SERVER URL”];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];
NSDictionary *postData = [[NSDictionary alloc] initWithObjectsAndKeys: @“TestUservalue”, @"name",
                     @“TestPassvalue”, @“password”,
                     nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject: postData options:0 error:&error];
[request setHTTPBody: postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[postDataTask resume];

答案 1 :(得分:0)

问题在于您要发送到网络服务器的参数。它们必须正确编码。我有相同的500状态代码。 我能够通过更改我的帖子参数来解决我的问题。我从我的帖子变量名中删除了_然后就可以了。

NSString * params = [NSString stringWithFormat:@"q_id=%@&c_id=%@&agent=%@",self.q_id, self.c_id, agent];

// changed to


NSString * params = [NSString stringWithFormat:@"qid=%@&cid=%@&agent=%@",self.q_id, self.c_id, agent];

Also check out this link on Objective-C encoding