C# 正在下载标头已断开的数据
我刚刚遇到了以下问题: 我需要下载一个登录页面后面的数据。然而,当我发出get请求时,服务器提供了错误的数据——内容在那里,但标题集中没有内容长度,它是一个空字段。我用Fiddler查找了它,当我尝试用browser下载文件时也是如此,但browser完成了下载ok,而C#在从我的请求中获取响应对象时例外地掉了下来 标题如下所示:C# 正在下载标头已断开的数据,c#,get,httpwebresponse,system.net.httpwebrequest,C#,Get,Httpwebresponse,System.net.httpwebrequest,我刚刚遇到了以下问题: 我需要下载一个登录页面后面的数据。然而,当我发出get请求时,服务器提供了错误的数据——内容在那里,但标题集中没有内容长度,它是一个空字段。我用Fiddler查找了它,当我尝试用browser下载文件时也是如此,但browser完成了下载ok,而C#在从我的请求中获取响应对象时例外地掉了下来 标题如下所示: HTTP/1.1 200 OK Date: Sat, 06 Dec 2014 11:55:06 GMT Server: Apache Content-Disposit
HTTP/1.1 200 OK
Date: Sat, 06 Dec 2014 11:55:06 GMT
Server: Apache
Content-Disposition: attachment; filename=;
Pragma: no-cache
Expires: 0
Content-Length:
X-Powered-By: PleskLin
Connection: close
Content-Type: application/octet-stream
Hersteller;"Hersteller Art-Nr";"Lieferant Art-Nr";Ma�stab;Bezeichnung;EAN;"EK (netto)";UVP;USt;Verkaufseinheit;Hinweis;"Letzte Pro...
我的代码如下所示
public string ReadPage(string path, string method = "GET"){
var result = "";
var request = (HttpWebRequest)WebRequest.Create(path);
request.Method = method;
request.Host = "somehost.de";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
request.Referer = @"http://somehost.de/login.php?redir=list.php%3Ftype%3Dmm";
request.AllowAutoRedirect = true;
request.Headers.Add("Cookie", LoginCookie);
try
{
var response = request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
result = sr.ReadToEnd();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//throw;
}
return result;
}
异常出现在var response=request.GetResponse()中代码>行。你知道怎么解决这个问题吗?我只想让它继续,让我读出数据
忘记了异常-这是一个带有消息的WebException
服务器违反了协议。Section=ResponseHeader Detail='Content-Length'标题值无效我非常喜欢
稍有变化
public static class WebRequestExtensions
{
public static WebResponse GetWEResponse(this WebRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
try
{
return request.GetResponse();
}
catch (WebException e)
{
return e.Response;
}
}
}
您现在可以通过使用request.GetWEResponse
public string ReadPage(string path, string method = "GET") {
var result = "";
var request = (HttpWebRequest)WebRequest.Create(path);
request.Method = method;
request.Host = "somehost.de";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
request.Referer = @"http://somehost.de/login.php?redir=list.php%3Ftype%3Dmm";
request.AllowAutoRedirect = true;
request.Headers.Add("Cookie", LoginCookie);
try
{
var response = request.GetWEResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
result = sr.ReadToEnd();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//throw;
}
return result;
}
或者更好
public string ReadPage(string path, string method = "GET")
{
var request = (HttpWebRequest)WebRequest.Create(path);
request.Method = method;
request.Host = "somehost.de";
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
request.Referer = @"http://somehost.de/login.php?redir=list.php%3Ftype%3Dmm";
request.AllowAutoRedirect = true;
request.Headers.Add("Cookie", LoginCookie);
using (var response = (HttpWebResponse)request.GetWEResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
异常消息是什么?请查看@metadings,尽管它是一个仍然遵守HTTP规则的正确发送的错误响应,而不是一个错误发送的成功响应。@JonHanna ha?这难道不是一个回应吗?@metadings OP没有得到回应,他们得到的是垃圾。看起来很有趣,你可以试试!然而,我遇到了另一个问题——我的结果是在运行Fiddler时得到的。当fiddler关闭时,我甚至没有在登录过程中走那么远,需要先解决这个问题。。。