C# WebRequest (400) 第二次调用错误请求
C# WebRequest (400) Bad Request on second call
我正在尝试使用程序检查我们的 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())
状态:协议错误
内部状态:RequestFatal
我解决了我的问题。问题是 keep-alive 属性,因为第二个 SSL 连接没有正确初始化。您必须将此行添加到您的请求中。
request.KeepAlive = false;
我正在尝试使用程序检查我们的 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())
状态:协议错误
内部状态:RequestFatal
我解决了我的问题。问题是 keep-alive 属性,因为第二个 SSL 连接没有正确初始化。您必须将此行添加到您的请求中。
request.KeepAlive = false;