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"]);}
Clinton Walsh
  • 167
  • 3
  • 14
  • What does `NSLog(@"http headers = %d", [[req allHTTPHeaderFields] count]);` print? – highlycaffeinated Aug 04 '11 at 18:22
  • you're not getting any headers back. you'll need to post more of your code for us to diagnose further. – highlycaffeinated Aug 04 '11 at 18:28
  • This is the method that I am using. – Clinton Walsh Aug 04 '11 at 18:32
  • `- (BOOL)webView:(UIWebView *)webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSMutableURLRequest *req = (NSMutableURLRequest *)request; NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey]; – Clinton Walsh Aug 04 '11 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"]); }` – Clinton Walsh Aug 04 '11 at 18:33
  • I started editing your post to add the method but realized it was incomplete. Can you edit your post and add the rest of the method? – highlycaffeinated Aug 04 '11 at 18:35

2 Answers2

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

Community
  • 1
  • 1
JohnK
  • 6,865
  • 8
  • 49
  • 75
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.

Community
  • 1
  • 1
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
  • 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)` – Clinton Walsh Aug 04 '11 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. – highlycaffeinated Aug 04 '11 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 – Clinton Walsh Aug 04 '11 at 19:11