3

我似乎无法让我的网络代码工作。在过去的两天里,我至少花了几个小时浏览文档和谷歌,但我无法弄清楚。无论出于何种原因,我似乎NSURLSession没有发送标题。我在 Postman for Chrome 中执行了正确的请求,并使用了这些确切的参数,所以我知道我没有发送不正确的数据。我继续收到一个错误,我可以通过删除标头参数在 Postman 中重现该错误。

代码:

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];

NSDictionary *sessionHeaders = [[NSDictionary alloc] initWithObjectsAndKeys:
@"application/json", @"Accept", @"application/xml", @"Content-Type", 
[NSString stringWithFormat:@"LivePerson appKey=%@", self.getAppKey], @"Authorization", nil];

sessionConfig.HTTPAdditionalHeaders = sessionHeaders;

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];

NSURLSessionDataTask *endpoints = [session dataTaskWithURL:[NSURL URLWithString:baseURL] 
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

NSDictionary *endpoints = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"%@", endpoints);
}];
4

1 回答 1

0

反过来……应该这样做

NSDictionary *sessionHeaders = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Accept", @"application/json", @"Content-Type", @"application/xml",  
@"Authorization", [NSString stringWithFormat:@"LivePerson appKey=%@", self.getAppKey], nil];
于 2014-09-17T13:58:50.580 回答