c#HttpWebRequest获取错误500但在浏览器中是正确的

时间:2016-07-10 05:12:51

标签: c#

当我使用此代码时,我收到错误500

发送respoce重定向到另一个网址时的此网址

但重定向请求类型为Post

WebRequest req = HttpWebRequest.Create("http://www.khanoumi.com/brandproductlist/allajax?id=204");
req.Method = "GET";
string response = "";
using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
{
    response = sr.ReadToEnd();
}

但是当我在浏览器中打开这个网址时它是正确的

1 个答案:

答案 0 :(得分:1)

第1步:向网址发出请求,禁用自动重定向,我们将从响应标头中获取重定向网址和Cookie

string host = "http://www.khanoumi.com";
WebHeaderCollection headers;
string response = "";

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create ("http://www.khanoumi.com/brandproductlist/allajax?id=204");
req.AllowAutoRedirect = false;
req.Method = "GET";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
req.Headers.Add ("Upgrade-Insecure-Requests", "1");
req.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
req.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36";

// reading the response
HttpWebResponse res = (HttpWebResponse)req.GetResponse ();
//getting new url and cookie of the header
headers = res.Headers;
string newUrl = host + headers.Get ("location");
string cookie = headers.Get("Set-Cookie").Split(';')[0];

第2步:使用新Cookie

向新网址发出请求
HttpWebRequest req2 = (HttpWebRequest)HttpWebRequest.Create (newUrl);
req2.Method = "GET";
req2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
req2.KeepAlive = true;
req2.Headers.Add ("Cookie", cookie);
req2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
req2.Headers.Add ("Upgrade-Insecure-Requests", "1");
req2.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36";
try{
    res = (HttpWebResponse)req2.GetResponse ();
    headers = res.Headers;
    foreach (string key in headers.AllKeys) {
        Console.WriteLine (key + ":" + headers.Get (key));
    }
    // you will get your result html in response variable
    using (StreamReader sr = new StreamReader(res.GetResponseStream())){
        response = sr.ReadToEnd();
    }           
    Console.WriteLine (response);

}catch(WebException ex){
    using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream())){
        response = sr.ReadToEnd();
    }           
    Console.WriteLine (response);
    Console.WriteLine (ex.Response);
}

注意:此处需要用户代理标头,重定向网址会检查其存在情况,这是500的主要原因