Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUrlRequest: where an app can find the default headers for HTTP request?

Does anybody know where an iOS app can see the default headers that NSUrlRequest sets for an HTTP request?

Just creating NSUrlRequest with "http://.." NSURL and then asking: [request allHTTPHeaderFields] returns an empty dictionary. But I know that for example "Accept-Encoding" is set to "gzip". So I want to get all that fields and show them in a HTTP request demo.

I've also tried to swizzle [NSMutableURLRequest setValue:forHTTPHeaderField:], but it seems that it is not used by underlying API (NSURLRequest or NSURLConnection) to set those default fields I'm hunting for.

I'm making just a simple iOS demo which shows HTTP request and response information, so it doesn't really matters if it will be a public or private API used for that.

like image 457
zubko Avatar asked Apr 17 '11 19:04

zubko


People also ask

Where are HTTP headers set?

In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name. In the Value box, type the custom HTTP header value.


2 Answers

Your app cannot. It's done all down in CFNetwork - Communicating with HTTP Servers. I believe it just adds missing header values not supplied by NSURLRequest.

The defaults are:

  • USER-AGENT "AppName - Eng/1.0 CFNetwork/485.13.9 Darwin/10.7.0"
  • ACCEPT "*/*"
  • ACCEPT-LANGUAGE "en-us"
  • ACCEPT-ENCODING "gzip, deflate"
  • CONNECTION "keep-alive"
like image 78
Black Frog Avatar answered Nov 15 '22 11:11

Black Frog


hmm... maybe you might want to try within

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

method in your custom nsurlconnection class. although the documentation mentions something about redirects, this is certainly worth looking into.

like image 22
govi Avatar answered Nov 15 '22 11:11

govi