Tag: 获取请求

NSURLConnection授权标头不工作

我试图通过NSURLConnection在HTTP头中发送一个OAuth访问令牌,但它似乎并没有发送头,因为API不断给我一个错误,说“必须提供授权令牌”。 这是我正在使用的代码: NSURL *aUrl = [NSURL URLWithString: @"http://generericfakeapi.com/user/profile"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [request addValue:[NSString stringWithFormat:@"OAuth %@", token] forHTTPHeaderField:@"Authorization"]; [request setHTTPMethod:@"GET"]; NSError *error = nil; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error: &error]; NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error]; NSLog(@"Response : %@", JSONDictionary); 这是API的cURL命令的一个例子: curl 'http://generericfakeapi.com/user/profile' -H 'Authorization: OAuth YourAuthToken' 这不是我通过NSURLConnection实质上是做什么? 任何帮助,将不胜感激。