I would like to append text to the default user-agent header in an NSURLConnection
. I know how to change the user-agent of the NSURLConnection, but I don't see how to get the default user-agent. I tried the following:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
userAgent = [userAgent stringByAppendingString:extraUserAgentInfo];
[request addValue:userAgent forHTTPHeaderField:@"User-Agent"];
This does not work because userAgent is coming back nil
from the valueForHTTPHeaderField:
call.
The User-Agent (UA) string is contained in the HTTP headers and is intended to identify devices requesting online content. The User-Agent tells the server what the visiting device is (among many other things) and this information can be used to determine what content to return.
A browser's User-Agent string (UA) helps identify which browser is being used, what version, and on which operating system. When feature detection APIs are not available, use the UA to customize behavior or content to specific browser versions.
Described in the HTTP standard, the User-Agent string contains a number of tokens that refer to various aspects of the request, including the browser's name and version, rendering engine, device's model number, operating system and its version, etc.
Part of the default User-Agent is your AppName and Version:
My user agent is:
User-Agent foo-bar/1.0 CFNetwork/609.1.4 Darwin/12.4.0
foo-bar
is the value of [[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleNameKey]
and
1.0
is the value of [[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleVersionKey]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With