Search code examples
c#httpwebhttpwebrequesthttpwebresponse

Loading and parsing an HTTP response that is stored in a file


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?


Solution

  • 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).