我想写一个登录脚本,但由于某种原因我得到一个内部服务器错误(500)。SSL HttpWebRequest导致内部服务器错误?
我试过用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());
}
}
}
有谁知道为什么我没有收到回复?
感谢您的帮助。
您是否收到任何错误?您是否检查了响应和responseStream的内容? – 2012-07-23 11:17:53
从webrequest获取响应时崩溃。 (内部服务器错误500)。 – oopbase 2012-07-23 11:19:01
无法解决您的问题,但是当您将方法设置为ServicePointManager.ServerCertificateValidationCallback时,请注意此设置为“全局”且不是线程安全的.... – 2012-07-23 11:19:13