【问题标题】:C# WebRequest (400) Bad Request on second callC# WebRequest (400) 第二次调用时出现错误请求
【发布时间】:2016-04-27 05:56:00
【问题描述】:

我正在尝试使用程序检查我们的 SSL 证书,但我遇到了问题;由于我不知道的东西,我不能使用第二个 HttpWebRequest。我认为这是一个缓存问题,并使用了我知道的所有无缓存代码。如您所见,服务器位于我们的私有子网中,并且 1 台服务器有很多 IIS 应用程序。因此,我将“request.Host”变量更改为我们匹配的 IIS 绑定以打开站点。总是第二个不起作用,但第一个效果很好。问题是什么?

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://" + "10.10.11.100");
            request.AllowAutoRedirect = false;
            request.Host = "www.xxx.com";
            request.Timeout = 30 * 1000;
            request.Headers.Add("Cache-Control", "no-cache");
            request.Referer = null;
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                //retrieve the ssl cert and assign it to an X509Certificate object
                X509Certificate cert = request.ServicePoint.Certificate;
                //convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
                X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert));
            }
            HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("https://" + "10.10.11.100");
            request2.AllowAutoRedirect = false;
            request2.Host = "www.yyy.com";
            request2.Timeout = 30 * 1000;
            request2.Headers.Add("Cache-Control", "no-cache");
            request2.Referer = null;

            request2.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse())
            {
                //retrieve the ssl cert and assign it to an X509Certificate object
                X509Certificate cert2 = request2.ServicePoint.Certificate;
                //convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
                X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert2));
            }

错误:

        The remote server returned an error: (400) Bad Request.

异常行:

using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse()) 

状态:协议错误

内部状态:请求致命

【问题讨论】:

  • 您是否尝试过交换订单以查看是否是第二台服务器的问题?
  • 是的,我做到了。如果我更改顺序,第一个将像以前一样工作。

标签: c# ssl httpwebrequest cache-control


【解决方案1】:

我解决了我的问题。问题是 keep-alive 属性,因为第二个 SSL 连接没有正确初始化。您必须将此行添加到您的请求中。

        request.KeepAlive = false;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2023-03-31
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    相关资源
    最近更新 更多