使用xcode获取HTTP头

使用xcode获取HTTP头,xcode,http,header,user-agent,nsurlrequest,Xcode,Http,Header,User Agent,Nsurlrequest,我正在尝试获取用户代理,但当我尝试读取它时,结果是(空) 请求是一个NSURLRequest。所以我尝试获取http头,但我认为没有。当我使用 NSLog(@"http headers = %d", [[req allHTTPHeaderFields] fileSize]); 它打印出零。req是一个NSMutableURLRequest。有人知道为什么会这样吗 这是我正在使用的方法: - (BOOL)webView:(UIWebView )webView2 shouldStartLoadW

我正在尝试获取用户代理,但当我尝试读取它时,结果是(空)

请求是一个NSURLRequest。所以我尝试获取http头,但我认为没有。当我使用

NSLog(@"http headers = %d", [[req allHTTPHeaderFields] fileSize]);
它打印出零。req是一个NSMutableURLRequest。有人知道为什么会这样吗


这是我正在使用的方法:

 - (BOOL)webView:(UIWebView )webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    NSMutableURLRequest *req = (NSMutableURLRequest *)request; 
    NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString)kCFBundleVersionKey];
NSLog(@"http headers = %@", [request allHTTPHeaderFields]);
NSLog(@"http headers = %d", [[req allHTTPHeaderFields] fileSize]);
[req setValue:[NSString stringWithFormat:@"myApp/%@ %@", versionString, [request valueForHTTPHeaderField:@"User-Agent"]] forHTTPHeaderField:@"User-Agent"];
    NSLog(@"user agent = %@", [request valueForHTTPHeaderField: @"User-Agent"]);}

您的请求对象中没有任何头,因为您没有添加任何头。如果您想指定
用户代理
标题,您需要自己添加它,如上所述。

这对我很有用:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSLog(@"navigator.userAgent = %@", secretAgent);

    NSDictionary* headers = [request allHTTPHeaderFields];
    NSLog(@"headers: %@",headers);

    NSString* ua = [request valueForHTTPHeaderField:@"User-Agent"];
    NSLog(@"User-Agent = %@", ua);
}
我不知道你为什么要看
filesize
,而你可以只看标题本身


参见
NSLog(@“http头=%d”,[[req allHTTPHeaderFields]计数])的作用打印?你没有得到任何标题。您需要发布更多代码以便我们进一步诊断。这是我正在使用的方法。`-(BOOL)webView:(UIWebView)webView2应该加载WithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型{NSMutableURLRequest*req=(NSMutableURLRequest*)请求;NSString*versionString=[[NSBundle mainBundle]objectForInfoDictionaryKey:(NSString)KCFBundLeverionKey];
NSLog(@“http头=%@,[request allHTTPHeaderFields]);NSLog(@“http头=%d”,[[req allHTTPHeaderFields]文件大小];[req setValue:[NSString stringWithFormat:@“myApp%@”,versionString,[request ValueForHttpHeaderFields:@“用户代理”]forHTTPHeaderField:@“用户代理”];NSLog(@“用户代理=%@”,[request valueForHTTPHeaderField:@“用户代理”];}
我尝试了该链接,但遇到了相同的问题。在我更改它之前,用户代理仍然是(空的)。`NSLog(@“用户代理=%@,[request valueForHTTPHeaderField:@“用户代理”)因此,当我向其中添加内容,然后再次运行NSLog时,我会在上面的代码中得到类似于
myApp/1.0(null)
的内容,您将用户代理设置为用户代理的当前值-即null。您必须提供自己的值。是否应该已经有用户代理。类似于Mozilla/5.0(Macintosh;英特尔Mac OS X 10.6;rv:5.0.1)Gecko/20100101 Firefox/5.0.1
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSLog(@"navigator.userAgent = %@", secretAgent);

    NSDictionary* headers = [request allHTTPHeaderFields];
    NSLog(@"headers: %@",headers);

    NSString* ua = [request valueForHTTPHeaderField:@"User-Agent"];
    NSLog(@"User-Agent = %@", ua);
}