【问题标题】:SSL HttpWebRequest causes internal server error?SSL HttpWebRequest 导致内部服务器错误?
【发布时间】: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


【解决方案1】:

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

从您的 cmets 收到 500 错误,这通常意味着如果 url/request 从一个脚本/应用程序而不是另一个脚本/应用程序工作,则它的格式不正确。这将使您看到正在请求的内容(并为您突出显示任何协议错误)。

【讨论】:

    【解决方案2】:

    我发现了错误...似乎服务器需要一个“有效”的用户代理。将 firefox 设置为用户代理时,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2015-11-23
      • 2013-08-11
      • 2012-03-29
      • 2014-04-14
      • 1970-01-01
      相关资源
      最近更新 更多