【发布时间】:2012-07-23 11:15:26
【问题描述】:
我正在尝试编写登录脚本,但由于某种原因,我收到了内部服务器错误 (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());
}
}
}
有人知道我为什么没有收到回复吗?
感谢您的帮助。
【问题讨论】:
-
您是否遇到任何错误?你检查过 response 和 responseStream 的内容吗?
-
从 webrequest 获取响应时崩溃。 (内部服务器错误 500)。
-
不能解决你的问题,但是当你给ServicePointManager.ServerCertificateValidationCallback设置一个方法时,请注意这个设置是“全局的”而不是线程安全的......
标签: c# ssl httpwebrequest