Objective c Cocoa:使用NSURLConnection访问Django站点中受保护的资源时,POST标头/参数丢失
我正在尝试使用NSURLConnection、OAuth2承载令牌和HTTPS访问Django站点上受保护的资源。我收到一个令牌,然后将其附加到GET参数、POST参数或头上。我可以访问响应GET参数的URL:s。但当我尝试使用POST访问URL时,服务器返回403,并显示一条自定义错误消息,表示没有包含令牌的header/POST参数。我尝试了几种解决方案和HTTP库。方法使用AFN网络,我试过了。由于苹果公司不喜欢修改“authorization”标题的警告,我们甚至将授权更改为接受另一个标题。我当前的代码如下:(scheme=@“https”) _Delegate handleData解析响应,而定制响应总是缺少承载令牌所需的header或post参数。Objective c Cocoa:使用NSURLConnection访问Django站点中受保护的资源时,POST标头/参数丢失,objective-c,django,cocoa,post,https,Objective C,Django,Cocoa,Post,Https,我正在尝试使用NSURLConnection、OAuth2承载令牌和HTTPS访问Django站点上受保护的资源。我收到一个令牌,然后将其附加到GET参数、POST参数或头上。我可以访问响应GET参数的URL:s。但当我尝试使用POST访问URL时,服务器返回403,并显示一条自定义错误消息,表示没有包含令牌的header/POST参数。我尝试了几种解决方案和HTTP库。方法使用AFN网络,我试过了。由于苹果公司不喜欢修改“authorization”标题的警告,我们甚至将授权更改为接受另一个标
看起来,即使我在每次重定向时用原始请求的可变副本替换请求,头/参数仍然会被NSURLConnection剥离。但是为什么,如何,因为我每次都发送一份原始请求的副本,并且我通过记录来验证它们是否存在 您是否尝试过使用http请求程序手动生成您的请求并查看它是否有效?试着看看你是否能得到一个有效的答复。我不是百分之百的确定你是否能够成功地做到这一点,因为你的承载令牌,但这只是一个建议,可能有助于消除一些可能性更新:我设法通过摸索代码(注释掉的部分)使POST参数通过,编码改为UTF8。但我还是不断失去头球。虽然我现在可以继续了,但我仍然想知道为什么NSURLConnection一直在筛选标题。如果我发现了什么,我会尝试你的方法并报告结果。很好,是的,让我知道:)
-(void) logOut {
NSString *pget = @"/api/logout/";
NSString *path = [pget stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *absolutePath = [NSString stringWithFormat:@"%@://%@%@", scheme, host, path];
NSURL *url = [NSURL URLWithString:absolutePath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
NSString *authValue = [NSString stringWithFormat:@"Bearer %@", accessToken];
[urlRequest setValue:authValue forHTTPHeaderField:@"Authorization_Extra"];
[urlRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"content-type"];
[urlRequest setHTTPMethod: @"POST"];
/*
NSString *post = [NSString stringWithFormat:@"access_token_extra=%@", accessToken];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody:postData];
*/
NSDictionary* headers = [urlRequest allHTTPHeaderFields];
NSLog(@"headers: %@",headers);
_originalRequest = urlRequest;
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:NO];
[connection start];
}
#pragma mark NSURLConnection Delegate Methods
- (NSURLRequest *)connection: (NSURLConnection *)connection
willSendRequest: (NSURLRequest *)request
redirectResponse: (NSURLResponse *)redirectResponse;
{
if (redirectResponse) {
// we don't use the new request built for us, except for the URL
NSURL *newURL = [request URL];
NSMutableURLRequest *newRequest = [_originalRequest mutableCopy];
[newRequest setURL: newURL];
NSLog(@"New Request headers: %@", [newRequest allHTTPHeaderFields]);
return newRequest;
} else {
return request;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
NSLog(@"Received response statuscode: %ld", (long)[response statusCode]);
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connection finished:");
[_delegate handleData:responseData];
}