Cocoa touch -[NSURLRequest sendSynchronousRequest:returningResponse:error:]正在获取HTTP头

Cocoa touch -[NSURLRequest sendSynchronousRequest:returningResponse:error:]正在获取HTTP头,cocoa-touch,Cocoa Touch,我试图从iPhone上的同步HTTP请求中提取HTTP头和HTTP响应代码。在异步请求中执行此操作通常没有任何问题,尽管这里有点麻烦。HTTP响应头为空,HTTP状态代码为0。Web服务器配置正确,我可以在异步请求中检索所需的详细信息。以下是复制到命令行程序的代码: 问题1:[httpResponse allHeaderFields]返回零。 问题2:[httpResponse statusCode]返回0 如果有一个错误,我现在看到的是:error Domain=NSURLErrorDomai

我试图从iPhone上的同步HTTP请求中提取HTTP头和HTTP响应代码。在异步请求中执行此操作通常没有任何问题,尽管这里有点麻烦。HTTP响应头为空,HTTP状态代码为0。Web服务器配置正确,我可以在异步请求中检索所需的详细信息。以下是复制到命令行程序的代码:

问题1:[httpResponse allHeaderFields]返回零。
问题2:[httpResponse statusCode]返回0

如果有一个错误,我现在看到的是:error Domain=NSURLErrorDomain Code=-1012 UserInfo=0x14b100“操作无法完成。(NSURLErrorDomain error-1012.)”,那么我无法访问http响应头/状态代码,这是不是理解错了

#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;
} 
#导入
int main(int argc,char*argv[])
{    
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
NSString*urlStr=@”http://tmp/myscript.php";
NSString*postBody=@“foo=bar”;
NSMutableURLRequest*请求;
NSData*postData=[postBody dataUsingEncoding:NSASCIIStringEncoding];
n错误*错误;
NSURLResponse*响应;
NSHTTPURLResponse*httpResponse;
NSData*数据回复;
身份证复印件;
请求=[NSMutableUrlRequestRequestWithURL:[NSURL URLWithString:urlStr]];
[请求设置HttpMethod:@“POST”];
[请求setHTTPBody:postData];
[请求设置值:@“文本/xml”用于HttpHeaderField:@“接受”];
[请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
dataReply=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
stringReply=(NSString*)[[NSString alloc]initWithData:dataReply编码:NSUTF8StringEncoding];
//一些调试代码等。
NSLog(@“来自服务器的回复:%@”,stringReply);
httpResponse=(NSHTTPURLResponse*)响应;
int statusCode=[httpResponse statusCode];
NSLog(@“HTTP响应头%@,[httpResponse allHeaderFields]);
NSLog(@“HTTP状态代码:%d”,状态代码);
//结束调试。
[[nsrunlop currentRunLoop]run];
[池释放];
返回0;
} 

您得到的错误代码是NSURErrorUserCancelledAuthentication,并记录在中。您可能试图加载受密码保护的页面,在这种情况下,您可能必须向URL添加用户名和密码

问题1:[httpResponse allHeaderFields]返回零。 问题2:[httpResponse statusCode]返回0


因为httpResponse为零,请参阅

你到底有什么问题?最后两条NSLog语句没有任何有用的内容。[httpResponse allHeaderFields]返回零。statusCode返回0。