How do I get the default user-agent string in an NSURLConnection? - iphone

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.

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]

There's a blog entry Changing the headers for UIWebKit HTTP requests from iCab. He suggest using a process call "Method Swizzling” to get what you want. But there is a rumor that Method swizzling cause for 4.0 rejection

Related

Alamofire support HTTP ETAG mechanism

I cannot understand if Alamofire support HTTP ETAG.
I find this discussion https://github.com/Alamofire/AlamofireImage/issues/5
and another thread on:
NSURLCache and ETags
Previously i use AFNETWORKING 1.x with Etag and i found this lines of code:
NSMutableURLRequest *mutableURLRequest = [self.request mutableCopy];
if ([self.response respondsToSelector:#selector(allHeaderFields)] && [[self.response allHeaderFields] valueForKey:#"ETag"]) {
[mutableURLRequest setValue:[[self.response allHeaderFields] valueForKey:#"ETag"] forHTTPHeaderField:#"If-Range"];
}
In AFHTTPRequestOperation.h (AFNETWORKING 1.x.x)
I cannot understand if Etag automatically work with url cache policy:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
Or in another way..
I'm just a little bit confused..

open browser from xcode program with post parameter prefilled

I am developing an application that integrates a payment gateway. This works by opening the page "https://sis.sermepa.es/sis/realizarPago" passing some parameters by POST, from there the customer can make the payment.
My problem is that I can not find how to open the browser giving a URL and parameters. I've seen that open the browser using Intents but does not allow parameters and I have seen that you can send POST parameters to a URL and wait for the response, but not open the URL itself.
Does anyone have the solution to my problem?
Thank you very much.
I've solved the problem, here you have the solution in case anyone else needs the answer.
NSString *code=codeTF.text;
NSString *price=priceTF.text;
NSString *email=emailTF.text;
NSString *body = [NSString stringWithFormat:Code=%#&Price=%#&E-mail=%#", code, price, email];
NSURL *url = [NSURL URLWithString:#"https://***/index.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
//initialize the webViem in the *.h
// #property (nonatomic, retain) UIWebView *webView;
[webView loadRequest:request];

Changing url for NSMutableURLRequest after its being initialized

I am trying to see if there is a way to change the request url once the NSMutableURLRequest is being initialize? I looked into the instance method, but nowhere it indicates that you can change the url. I tried to make a request of "URL" and change it accordingly, but it still points to the old url.
Anyone know how to change this would like to know it.
Thanks.
There is a method on NSMutableURLRequest called - (void)setURL:(NSURL *)theURL
Here's an example:
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
NSURL *anotherURL = [NSURL URLWithString:#"http://www.yahoo.com"];
[req setURL:anotherURL];
Specifically, the method documentation is at:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableURLRequest/setURL:

Specifying HTTP referer in embedded UIWebView

In my app, I allow the user to open up an external page in an embedded UIWebView. Is it possible for me to set the referer header that's sent with that request? I'd like for my app to get the 'cred' when the user opens up these external pages.
Set the referer using - setValue:forHTTPHeaderField:
NSMutableURLRequest* request = ...;
[request setValue:#"https://myapp.com" forHTTPHeaderField: #"Referer"];
But note that according to the HTTP RFC you shouldn't, because your app is not addressable using a URI:
The Referer field MUST NOT be sent if the Request-URI was obtained
from a source that does not have its own URI, such as input from the
user keyboard.
... unless you are using a custom protocol binded to your app (myapp://blah.com/blah).
You can create one and call loadRequest: manually or intercepting a normal request made by the user.
- (BOOL) webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType) navigationType
{
NSDictionary *headers = [request allHTTPHeaderFields];
BOOL hasReferer = [headers objectForKey:#"Referer"]!=nil;
if (hasReferer) {
// .. is this my referer?
return YES;
} else {
// relaunch with a modified request
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [request URL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
[request setValue:#"https://whatever.com" forHTTPHeaderField: #"Referer"];
[self.webView loadRequest:request];
});
});
return NO;
}
}
I haven't used this myself, but it looks like NSURLProtocol is the approved way to intercept and modify URL requests. Here's a tutorial: http://www.raywenderlich.com/59982/nsurlprotocol-tutorial
I'm using your solution of casting the request to NSMutableURLRequest, but since it's not documented that this is a mutable request, there's some risk that Apple might use an immutable object in the future.

NSURLConnection or NSurl?

what is the difference between NSURLConnection and NSURL?
i mean if i am downloading a file, does it make and difference which one i use?
Rgds
for:
NSString *myUrl = #"http://www.test.com/";
NSString *returnData = [NSString stringWithContentsOfURL:[NSURL URLWithString: myUrl]];
or
NSString *myUrl = #"http://www.test.com/";
NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString:myUrl] ];
NSString *returnData = [NSURLConnection sendSynchronousRequest:myRequest returningResponse: nil error: nil ];
whats the difference?
thks
The Connection
An NSURLConnection object provides support to perform the loading of a URL request.
The Request
NSURLRequest objects represent a URL load request in a manner independent of protocol and URL scheme.
E.g. requestWithURL:
Creates and returns a URL request for a specified URL with default cache policy and timeout value.
+ (id)requestWithURL:(NSURL *)theURL
The URL
The NSURL class provides a way to manipulate URLs and the resources they reference. NSURL objects understand URLs as specified in RFCs 1808, 1738, and 2732. ...
To get the contents of a URL, NSString provides stringWithContentsOfURL: and NSData provides dataWithContentsOfURL:.
References:
NSURLConnection_Class/Reference/Reference.html
NSURL_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURL
Best thing about NSURLConnection is its asynchronous behaviour so that you dont have to wait until the url is loaded.