簡體   English   中英

閱讀HttpWebResponse時獲取“Stream not readable”

[英]Getting “Stream not readable” when reading HttpWebResponse

我正試圖“屏蔽”我收到的一些數據如下(來自提琴手)

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

(內容是我的憑據) - 此標題與我在瀏覽器中手動查看網頁時追蹤的請求相匹配。

我使用HttpWebRequest.GetResponse()發送此消息我得到了回復

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

這(再次)與我使用瀏覽器得到的相匹配。 使用fiddler,我可以看到17k的數據(html),我試着用它讀...

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;
}

我在“new StreamReader”行上得到一個例外,說明“Stream不可讀”我的調試器向我顯示該流既不可讀也不可寫。 這是我讀過的第二頁(第一頁是對主URL的簡單請求以獲取登錄頁面)。 使用相同的代碼可以正常工作。 所有流和請求在使用后都已關閉。

谷歌沒有給我任何幫助,建議頁面錯誤(不,狀態為200)或已經讀取數據(不,代碼直接在這里)我根本不使用線程,所以這不是問題。 我試過改變編碼(請求gzip按預期給我一個較小的有效載荷,但我仍然無法讀取它)我在win7 x64上使用c#.net 4.5.2

有誰知道我做錯了什么?

D'哦! 由於代碼中的“疏忽”,我試圖讀取流兩次! 一切正常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM