【发布时间】:2015-07-22 13:19:38
【问题描述】:
我正在尝试“筛选”一些数据 我有如下要求(来自提琴手)
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;
}
我在“新 StreamReader”行出现异常,指出“流不可读” 我的调试器显示流既不可读也不可写。这是我读过的第二页(第一页是对主 url 的简单请求以获取登录页面)。使用相同的代码可以正常工作。所有流和请求在使用后都已关闭。
Google 没有给我任何帮助,提示页面错误(不,状态为 200)或已经读取数据(不,代码直接在这里) 我根本没有使用线程,所以这不是问题。 我已经尝试更改编码(请求 gzip 给了我预期的更小的有效负载,但我仍然无法读取它) 我在 win7 x64 上使用 c#.net 4.5.2
有人知道我做错了什么吗?
【问题讨论】:
-
仅供参考,使用 using 语句时无需关闭阅读器。一旦 using 语句失去作用域,阅读器将自动关闭并处理。即使您的 return 在其中,一旦您返回,它也被视为超出范围。
-
你试过用
StreamReader.Read()代替StreamReader.ReadToEnd()吗?也许“结束”是未知的。 -
我没有但是...检查流(使用调试器)显示流不可读,我怀疑这会有所帮助
-
回答了类似的情况,可能会有所帮助:stackoverflow.com/a/43981770/2791237
标签: c# http c#-4.0 system.net.httpwebrequest