下载标头损坏的数据

本文关键字:数据 损坏 下载 | 更新日期: 2023-09-27 18:33:24

我刚刚遇到了以下问题:

我需要下载登录页面后面的数据。但是,当我发出 get 请求时,服务器提供了错误的数据 - 内容在那里,但标题集中没有内容长度,它是一个空字段。我用 Fiddler 查找了它,当我尝试使用浏览器下载文件时也是如此,但浏览器完成下载正常,而 C# 在从我的请求中获取响应对象时异常丢弃。

标题如下所示:

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服务器违反了协议。部分=响应标头详细信息="内容长度"标头值无效

下载标头损坏的数据

我真的很喜欢马修的答案:.Net HttpWebRequest.GetResponse() 在返回 http 状态代码 400(错误请求)时引发异常

利特位已更改

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