1

I am trying to get the user agent but when I try and read it, it comes out (null)

NSLog(@"user agent = %@", [request valueForHTTPHeaderField: @"User-Agent"]);

request is an NSURLRequest. So I tried to get the http headers and I don't think there are any. When I use

NSLog(@"http headers = %d", [[req allHTTPHeaderFields] fileSize]);

it prints out zero. req is an NSMutableURLRequest. Does anyone know why this is happening.

This is the method that I am using:

 - (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"]);}
6
  • What does NSLog(@"http headers = %d", [[req allHTTPHeaderFields] count]); print? Aug 4, 2011 at 18:22
  • you're not getting any headers back. you'll need to post more of your code for us to diagnose further. Aug 4, 2011 at 18:28
  • This is the method that I am using. Aug 4, 2011 at 18:32
  • `- (BOOL)webView:(UIWebView )webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSMutableURLRequest *req = (NSMutableURLRequest *)request; NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString)kCFBundleVersionKey]; Aug 4, 2011 at 18:32
  • 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"]); } Aug 4, 2011 at 18:33

2 Answers 2

2

This worked for me:

- (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);
}

I don't know why you're looking at filesize when you can just look at the headers themselves.

Cf. https://stackoverflow.com/a/19184414/1431728

0

You don't have any headers in your request object because you haven't added any. If you want to specify the User-Agent header, you need to add it yourself, as outlined here.

3
  • I tried that link and I got the same problem. The user agent is still (null) before I change it. ` NSLog(@"user agent = %@", [request valueForHTTPHeaderField: @"User-Agent"]);` So when I add stuff to it and then run the NSLog again I get something that looks like myApp/1.0 (null) Aug 4, 2011 at 18:58
  • in the code above, you are setting the User-Agent to the current value of User-Agent - which is null. You have to supply your own value. Aug 4, 2011 at 19:05
  • Shouldn't there already be a user-agent. Something like Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0.1) Gecko/20100101 Firefox/5.0.1 Aug 4, 2011 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.