我在将HttpWebRequest发送到特定网址时遇到问题。 生成响应但不是我想要的。 它会给出类似"会话超时的响应!请登录以继续"。 然而, 当我通过Python 3.4发送相同的请求时,它会给我所需的响应。 响应就像" - 有些html在这里 - "我想要的。
我不知道为什么会这样,因为我不想通过C#来做。
CodeBehind(给出错误的回复)是:
string postData = "somequerystring";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("someurl");
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version10;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
request.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
using (var dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseFromServer = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
Context.Response.Write(responseFromServer);
Python脚本(给出正确的答案)是:
import requests
url = "someurl"
payload = "somequerystring"
headers = {
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
帮帮我吧?