Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Stream not readable" when reading HttpWebResponse

I'm trying to "screen scrape" some data I have a request as follows (from fiddler)

POST http://fallenlondon.storynexus.com/Auth/EmailLogin HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Referer: http://fallenlondon.storynexus.com/signup
User-Agent: Mine
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Cache-Control: max-age=0
l: 
Origin: http://fallenlondon.storynexus.com/
DNT: 1
Accept-Encoding: utf-8
Accept-Language: en-GB,en;q=0.8
Cookie: ASP.NET_SessionId=05xq3gndu4nczvy5wsah5qyw; __utma=100212060.1740063036.1431282067.1431282067.1431284767.2; __utmb=100212060.14.10.1431284767; __utmc=100212060; __utmz=100212060.1431282067.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host: fallenlondon.storynexus.com
Content-Length: 54

(The content is my credentials) - This header matches the request I traced from manually viewing the web page in my browser.

I send this using HttpWebRequest.GetResponse() I get a response back

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-cache, no-store, must-revalidate
Content-Type: text/html; charset=utf-8
Date: Mon, 11 May 2015 20:54:15 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 4.0
X-Powered-By: ASP.NET
X-Server: Web1
Content-Length: 16900
Connection: keep-alive

This (again) matches what I get using the browser. Using fiddler, I can see the 17k of data (html), I try to read it using...

var stream = response.GetResponseStream();
if (stream == null) return null;
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
   var data = reader.ReadToEnd();
   reader.Close();
   return data;
}

I get an exception on the "new StreamReader" line stating "Stream is not readable" My debugger shows me that the stream is neither readable or writeable. This is the 2nd page I have read (the first one being a simple request to the main url to get the login page). This works fine using the same code. All streams and requests have been closed after use.

Google gives me no help, suggesting a page error (no, status is 200) or having already read the data (no, code goes straight here) I am not using threading at all so that's not the issue. I have tried changing the encoding (requesting gzip gives me a smaller payload as expected but I still can't read it) I am using c#.net 4.5.2 on win7 x64

Does anyone have any idea what I'm doing wrong?

like image 562
Taoist Avatar asked Sep 27 '22 23:09

Taoist


1 Answers

D'oh! Due to an "oversight" in the code, I was trying to read the stream twice! All working now

like image 135
Taoist Avatar answered Oct 12 '22 23:10

Taoist