使用带有POST的NSURLSession无法从Web服务获取数据

时间:2015-12-10 13:35:47

标签: ios nsurlconnection watchkit

当我尝试在Apple Watch上使用NSURLSession使用POST调用Web服务时,响应参数为nil。这可能是什么问题?

调用网络服务的代码:

NSURL *url = [NSURL URLWithString:@"https://demo.test.com:22322/api/Alerts/UpdateAlertStatus?intUserId=5&paramaccessTokenId=24460c5f-be71-45b5-99cf-f46c277c3d9e&UserId=100200"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";

NSMutableDictionary *mutDictParameters = [[NSMutableDictionary alloc] init];

[mutDictParameters setObject:@"708"             forKey:@"AlertId"];
[mutDictParameters setObject:@"2878"            forKey:@"XmlMessageId"];
[mutDictParameters setObject:@"5"               forKey:@"UserId"];

NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:mutDictParameters
                                               options:kNilOptions error:&error];
if (!error) {
    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                               fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
                                                                   // Handle response here
                                                                   NSLog(@"Response : %@",response);
                                                               }];
    [uploadTask resume];
}

0 个答案:

没有答案