为什么我在浏览器上获得的响应不同于服务器

时间:2011-05-31 22:52:58

标签: c# asp.net

以下是一个示例网站

http://us.blizzard.com/store/browse.xml?f=c:5,c:33

当我在Firefox中检查响应时,它是application / xhtml

当我使用以下标题向同一个url服务器端发出请求时

var request = (HttpWebRequest)WebRequest.Create(url);
var cookieContainer = new CookieContainer();

request.CookieContainer = cookieContainer;
request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
request.Method = "GET";
request.AllowAutoRedirect = true;
request.Timeout = 15000;

响应是application / xml

任何想法?

由于

1 个答案:

答案 0 :(得分:4)

尝试在您的请求中添加Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

request.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

编辑:

尝试从Firefox复制请求。

我试过这个(我使用chrome + chrome dev工具来获取标题)

        request.CookieContainer = cookieContainer;
        request.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.68 Safari/534.30";
        request.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
        request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        request.Headers.Add("Accept-Language", "en-US,en;q=0.8");

        request.Method = "GET";
        request.AllowAutoRedirect = true;
        request.Timeout = 15000;

然后回来application/xhtml+xml