2012-07-23 61 views
0

我想写一个登录脚本,但由于某种原因我得到一个内部服务器错误(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()); 
      } 
     } 
    } 

有谁知道为什么我没有收到回复?

感谢您的帮助。

+0

您是否收到任何错误?您是否检查了响应和responseStream的内容? – 2012-07-23 11:17:53

+0

从webrequest获取响应时崩溃。 (内部服务器错误500)。 – oopbase 2012-07-23 11:19:01

+0

无法解决您的问题,但是当您将方法设置为ServicePointManager.ServerCertificateValidationCallback时,请注意此设置为“全局”且不是线程安全的.... – 2012-07-23 11:19:13

回答

0

我发现这个错误...好像服务器需要一个“有效的”useragent。将firefox设置为useragent时,一切正常。

0

我建议使用Fiddler检查HTTP请求/响应。您也可以将其设置为拦截HTTPS流量。这样你可以看到你的系统正在请求什么。

从您的意见中得到一个500错误,这通常意味着url/request是畸形的,如果它从一个脚本/应用程序,而不是其他。这将让你看到什么是请求(并突出显示任何协议错误)。

相关问题