2

I have a file stored locally that contains the response of a http request. It looks something like this:

HTTP/1.1 200 OK
Accept-Ranges: bytes
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin: http://www.pokemon.com
Cache-Control: max-age=3600, public
Content-Type: image/png
Date: Tue, 19 Apr 2016 17:33:04 GMT
Etag: "2542947452"
Expires: Tue, 26 Apr 2016 17:33:04 GMT
Last-Modified: Wed, 04 Jun 2014 14:59:14 GMT
Server: ECAcc (dca/247E)
X-Cache: HIT
Content-Length: 335685

‰PNG


IHDR   õ  V   ±NÛ‘      pHYs     šœ    cHRM  z%  €ƒ  ùÿ  €é  u0  ê`  :˜  o’_ÅF ËIDATx ÿÿ  ÿâd  ÿ              ÿ    _   n ÿ #                ÿ      ÿ                          ÿ          ÿÿ    ÿ                              ÿ      ÿ                      ÿ          ÿ            ÿ        ÿ      ÿ            ÿ        ÿ                      ÿ          ÿ          ÿ    ÿ        ÿ                      ÿ                              ÿ                ÿ    ÿ                  

I need to extract that payload and store it in a separate file (in this case a png file) I cannot figure out how to load it in memory and then parsing it accordingly in a HttpWebResponse or similar, so I can access the properties easily.

I thought I could use WebRequest.Create to open the file but I'm obviously missing something else because that only accepts a url. Anybody could give me some hints?

LEM
  • 825
  • 6
  • 16
  • 31

2 Answers2

1

This is a raw HTTP response as you surmised. There is a blank line after the headers, and the remainder is the entity payload. If you save the payload part to a file, that will be a PNG, which should be 335685 bytes long (or, to put it another way, remove the blank line and everything above it).

Jacob
  • 77,566
  • 24
  • 149
  • 228
1

Interesting question, I spent some time digging around https://github.com/dotnet/runtime looking into how a System.Net.Http HttpWebResponse is created but I can't see an easy way to construct one from a stream.

Here is where a HttpResponseMessage is created and parsed, which seems to be tightly coupled with the SafeWinHttpHandle representing the underlying network socket rather than an IO.Stream.

Most of the test cases I looked at would construct a response, or parse a single line, rather than the whole response.

I think your best option for reusing System.Net.Http's response parser is to create a loop back socket that returns the file. And a HttpClient to fake a request.

Jeremy Lakeman
  • 9,515
  • 25
  • 29