0
我已使用以下代碼通過NSURLSession
發送請求,但它返回的結果不正確。但如果我檢查與POSTMAN相同的參數和網址,那麼它工作正常。請幫忙。NSURLSessionDataTask返回不正確的結果
// URL of the endpoint we're going to contact.
NSURL *url = url;
// Create a simple dictionary with numbers.
NSDictionary *param = @{@"key1": @"value1"};
NSError *error = nil;
// Convert the dictionary into JSON data.
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:param
options:kNilOptions error:&error];
// Create a POST request with our JSON as a request body.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = JSONData;
// Create a task.
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
if (!error)
{
if (data) {
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
success(json);
}
else {
failure(error);
NSLog(@"******************* error in sendRequestToServer");
}
//NSLog(@"Status code: %i", ((NSHTTPURLResponse *)response).statusCode);
}
else {
failure(error);
NSLog(@"******************* error in sendRequestToServer");
}
}];
// Start the task.
[task resume];
什麼不正確的結果?你怎麼知道這是不同的?你進入一個錯誤的情況? – Larme
它沒有提供任何錯誤。但結果不同於POSTMAN –
有多少不同?錯誤的結果? – Larme