How to get a full request from NSURLRequest?

Is there a way to get the actual data to be sent when the NSURLConnection sends the NSURLRequest? Right now I'm mostly interested in HTTP and HTTPS requests, but since NSURLRequest works for many protocols, there seems to be a general way to see the relevant data for any type of request.

I missed something or I need to build a query myself by combining headers, body, etc.

To be clear, I would like to do this programmatically, rather than watching what appears on the server.

Update: At this point, I wrote code that effectively creates a query using data in NSURLRequest, so I am not asking how to do this. However, I would still like to know if there is a way to access the request stream that the URL loading system will generate. In other words, can I access the exact bytes to be sent if the NSURLConnection should have sent my NSURLRequest?

+4
source share
2 answers

Am I missing something, or do I need to build a query myself by combining headers, bodies, etc.

It turns out that this is exactly what I needed to do, and it makes sense. This is a protocol handler (a subclass of NSURLProtocol), not NSURLRequest, which knows how to format a request for its specific protocol. Fortunately, it is very simple to map an instance of NSURLRequest to a valid HTTP request. You can get the method (GET, POST, etc.), the requested resource and host from the request, and also get all HTTP headers using the -allHTTPHeaderFields method, which returns a dictionary. Then you can iterate over the keys in the dictionary and add lines to the query, for example:

 for (NSString *h in headers) { [renderedRequest appendFormat:@"%@: %@\r\n", h, [headers objectForKey:h]]; } 
+1
source

According to your last question:

  • Check cookieCenter.
  • Check storage credentials.
  • Register all headers, for example, according to the first delegate method urlConnection didReceiveResponse:
  • Make this connection to the local web service and try to catch all the headers, parameters, etc.

In addition, the easiest way is to make a request yourself.

+1
source

Source: https://habr.com/ru/post/1394264/


All Articles