C# WebRequest (400) 第二次调用错误请求

标签 c# ssl httpwebrequest cache-control

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

            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.

异常(exception)行:

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

状态:协议(protocol)错误

内部状态:请求致命

最佳答案

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

        request.KeepAlive = false;

关于C# WebRequest (400) 第二次调用错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36881385/

相关文章:

c# 分配引用

c# - 如何从 C# 中的数据集中获取日期?

c# - 停止错误发布编译的优雅方法

c# - 通过 SSL 使用 HttpWebRequest 发送请求时出现错误 502(错误的网关)

asp.net - 在asp.net中通过Web API上传大文件

c# - 谁能解释这个枚举器语法?

ssl - Neo4j 和 LetsEncrypt

java - Tomcat SSL 不同的 keystore 和 key 密码

windows - 配置 https 非常简单 - 使用 openssl

c# - SSL HttpWebRequest 导致内部服务器错误?