-[NSURLRequest sendSynchronousRequest:returningResponse:error:]返回HTTP标头 [英] -[NSURLRequest sendSynchronousRequest:returningResponse:error:] getting back HTTP headers
问题描述
我正在尝试从iPhone上的同步HTTP请求中提取HTTP标头和HTTP响应代码.对于异步请求,我通常没有任何问题,尽管这里有些麻烦.HTTP响应标头为null,HTTP状态码为0.Web服务器配置正确,我可以在异步请求中检索所需的详细信息.这是我的代码,复制到命令行程序中:
I'm trying to pull out HTTP Headers and an HTTP Response Code from a synchronous HTTP request on the iPhone. I generally don't have any issues doing this with asynchronous requests, although a bit of trouble here. The HTTP Response Headers are null and HTTP Status Code is 0. Web server is configured proper and I can retrieve the details I need in an asynchronous requests. Here's my code copied out to a command line program:
问题1:[httpResponse allHeaderFields]返回nil.
问题2:[httpResponse statusCode]返回0.
Problem #1: [httpResponse allHeaderFields] returns nil.
Problem #2: [httpResponse statusCode] returns 0.
我是否理解错误,我现在看到的错误是:Error Domain = NSURLErrorDomain代码= -1012 UserInfo = 0x14b100操作无法完成.(NSURLErrorDomain错误-1012.)",我无法访问http响应标头/状态代码?
Am I wrong in understanding that if there is an error, which I'm now seeing as: Error Domain=NSURLErrorDomain Code=-1012 UserInfo=0x14b100 "Operation could not be completed. (NSURLErrorDomain error -1012.)", that I do not get access to http response headers/status code?
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *urlStr = @"http://tmp/myscript.php";
NSString *postBody = @"foo=bar";
NSMutableURLRequest *request;
NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding];
NSError *error;
NSURLResponse *response;
NSHTTPURLResponse *httpResponse;
NSData *dataReply;
id stringReply;
request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody:postData];
[request setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
stringReply = (NSString *)[[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
// Some debug code, etc.
NSLog(@"reply from server: %@", stringReply);
httpResponse = (NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode];
NSLog(@"HTTP Response Headers %@", [httpResponse allHeaderFields]);
NSLog(@"HTTP Status code: %d", statusCode);
// End debug.
[[NSRunLoop currentRunLoop] run];
[pool release];
return 0;
}
推荐答案
您获得的错误代码为NSURLErrorUserCancelledAuthentication,并记录在
The error code you got is NSURLErrorUserCancelledAuthentication and is documented in Foundation Constants Reference. You might be trying to load a password-protected page, in which case you may have to add a username and password to the URL.
这篇关于-[NSURLRequest sendSynchronousRequest:returningResponse:error:]返回HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!