[英]SSL HttpWebRequest causes internal server error?
我正在尝试编写登录脚本,但是由于某种原因,我遇到了内部服务器错误(500)。
我用PHP和cURL进行了尝试,设置选项VERIFY_PEER = false
时得到了响应。
这是C#代码:
private void Login()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://whatever.com");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = string.Format("user={0}&password={1}&submit=login", User, Password);
byte[] data = Encoding.ASCII.GetBytes(postData);
webRequest.Method = "POST";
ServicePointManager.ServerCertificateValidationCallback = (x,y,z,a) => true;
webRequest.ContentLength = data.Length;
webRequest.KeepAlive = false;
using (Stream stream = webRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(responseStream.ReadToEnd());
}
}
}
有人知道我为什么没有得到答复吗?
谢谢你的帮助。
我建议使用Fiddler检查HTTP请求/响应。 您也可以将其设置为拦截HTTPS通信。 这样一来,您可以准确地看到系统的要求。
从您的评论中,您将得到500错误,这通常意味着url /请求如果在一个脚本/应用程序而非其他脚本/应用程序中均有效,则格式错误。 这将使您看到正在请求的内容(并为您突出显示任何协议错误)。
我发现了错误...好像服务器需要一个“有效的”用户代理。 将firefox设置为用户代理时,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.