c#HttpWebResponse头编码

Ale*_*s B 8 c# encoding header httpwebresponse

我有以下问题.我联系了一个我知道使用301重定向的地址.

使用HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl); ,loHttp.AllowAutoRedirect = false;所以我没有重定向.

现在我得到响应的标题以识别新的URL.

运用 loWebResponse.GetResponseHeader("Location");

问题是,由于此URL包含希腊字符,因此返回的字符串全部混乱(由于编码).

完整图片代码:

HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.ContentType = "application/x-www-form-urlencoded";
loHttp.Method = "GET";

Timeout = 10000;

loHttp.AllowAutoRedirect = false;
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();

string url= loWebResponse.Headers["Location"];
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 6

如果您让默认行为(loHttp.AllowAutoRedirect = true)和您的代码不起作用(您没有被重定向到新资源),则意味着服务器没有Location正确编码标头.重定向是否在浏览器中工作?

例如,如果重定向网址是http://site/???_??????Location标头,则必须如此 http://site/%CE%95%CE%BD%CE%B9%CE%B1%CE%AF%CE%BF_%CE%94%CE%B5%CE%.


更新:

进一步研究了这个问题之后我开始怀疑,有一些HttpWebRequest.发送请求时,服务器发送以下响应:

HTTP/1.1 301 Moved Permanently
Date: Fri, 11 Dec 2009 17:01:04 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Location: http://www.site.com/buy/??????-???????-?????????/c/cn69569/
Content-Length: 112
Content-Type: text/html; Charset=UTF-8
Cache-control: private
Connection: close
Set-Cookie: BIGipServerpool_webserver_gr=1007732746.36895.0000; path=/


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Run Code Online (Sandbox Code Playgroud)

我们可以看到Location标题包含非url编码的希腊字符.根据HTTP规范,我不太确定这是否有效.我们可以肯定地说,网络浏览器正确地解释了它.

这是有趣的部分.似乎HttpWebRequest不使用UTF-8编码来解析响应头,因为在分析Location头时它会给出:http://www.site.com/buy/κινηÏή-ÏÏαθεÏή-ÏηλεÏÏνία/c/cn69569/当然是错误的,当它尝试重定向到这个位置时,服务器会响应一个新的重定向,依此类推,直到达到最大重定向数并抛出异常.

我找不到任何方法来指定HttpWebRequest解析响应头时使用的编码.如果我们手动使用TcpCLient,它可以很好地工作:

using (var client = new TcpClient())
{
    client.Connect("www.site.com", 80);

    using (var stream = client.GetStream())
    {
        var writer = new StreamWriter(stream);
        writer.WriteLine("GET /default/defaultcatg.asp?catg=69569 HTTP/1.1");
        writer.WriteLine("Host: www.site.com");
        writer.WriteLine("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090805 Shiretoko/3.5.2");
        writer.WriteLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        writer.WriteLine("Accept-Language: en-us,en;q=0.5");
        writer.WriteLine("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        writer.WriteLine("Connection: close");
        writer.WriteLine(string.Empty);
        writer.WriteLine(string.Empty);
        writer.WriteLine(string.Empty);
        writer.Flush();

        var reader = new StreamReader(stream);
        var response = reader.ReadToEnd();
        // When looking at the response it correctly reads 
        // Location: http://www.site.com/buy/??????-???????-?????????/c/cn69569/
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我对这种行为感到很困惑.有没有办法指定使用的正确编码HttpWebRequest?也许应该设置一些请求标头?

作为一种解决方法,您可以尝试修改asp执行重定向和urlencode Location标头的页面.例如,当您在ASP.NET应用程序中执行a时Response.Redirect(location),该位置将自动进行html编码,并且任何非标准字符都将转换为其对应的实体.

例如,如果您执行以下操作:Response.Redirect("http://www.site.com/buy/??????-???????-?????????/c/cn69569/");在ASP.NET应用程序中,Location标头将设置为:

http://www.site.com/buy/%ce%ba%ce%b9%ce%bd%ce%b7%cf%84%ce%ae-%cf%83%cf%84%ce%b1%ce%b8%ce%b5%cf%81%ce%ae-%cf%84%ce%b7%ce%bb%ce%b5%cf%86%cf%89%ce%bd%ce%af%ce%b1/c/cn69569
Run Code Online (Sandbox Code Playgroud)

似乎这不是经典ASP的情况.

  • 在.Net中,头文件的解析以"纯ASCII"编码处理,该编码封装在WebHeaderCollection类中.这符合RFC 2616.通过假设字符集是UTF-8(实际的八位字节流中的内容),无论是谁发布位置标题都是错误的,但大多数浏览器"只处理它". (2认同)