通过 SSL 使用 HttpWebRequest 发送请求时出现错误 502(错误网关)
我在经典 ASP 中有以下代码片段,用于通过 SSL 发送命令并检索响应:
Dim xmlHTTP
Set xmlHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
xmlHTTP.open "POST", "https://www.example.com", False
xmlHTTP.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlHTTP.setRequestHeader "Content-Length", Len(postData)
xmlHTTP.Send postData
If xmlHTTP.status = 200 And Len(message) > 0 And Not Err Then
Print xmlHTTP.responseText
End If
然后我使用 此代码作为在 c# 中重新实现请求的参考:
private static string SendRequest(string url, string postdata)
{
WebRequest rqst = HttpWebRequest.Create(url);
// We have a proxy on the domain, so authentication is required.
WebProxy proxy = new WebProxy("myproxy.mydomain.com", 8080);
proxy.Credentials = new NetworkCredential("username", "password", "mydomain");
rqst.Proxy = proxy;
rqst.Method = "POST";
if (!String.IsNullOrEmpty(postdata))
{
rqst.ContentType = "application/x-www-form-urlencoded";
byte[] byteData = Encoding.UTF8.GetBytes(postdata);
rqst.ContentLength = byteData.Length;
using (Stream postStream = rqst.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
postStream.Close();
}
}
((HttpWebRequest)rqst).KeepAlive = false;
StreamReader rsps = new StreamReader(rqst.GetResponse().GetResponseStream());
string strRsps = rsps.ReadToEnd();
return strRsps;
}
问题是,当调用 GetRequestStream 时,我不断收到 WebException 并显示消息 “远程服务器返回错误: (502) Bad Gateway."
起初我以为这与 SSL 证书验证有关。所以我添加了这一行:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
Where
public class AcceptAllCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
System.Security.Cryptography.X509Certificate certificate,
WebRequest request,
int certificateProblem)
{
return true;
}
}
并且我不断收到相同的 502 错误。有什么想法吗?
I have the following snippet in classic ASP, to send a command and retrieve the response over SSL:
Dim xmlHTTP
Set xmlHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
xmlHTTP.open "POST", "https://www.example.com", False
xmlHTTP.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlHTTP.setRequestHeader "Content-Length", Len(postData)
xmlHTTP.Send postData
If xmlHTTP.status = 200 And Len(message) > 0 And Not Err Then
Print xmlHTTP.responseText
End If
Then I used this code as a reference to reimplement the request in c#:
private static string SendRequest(string url, string postdata)
{
WebRequest rqst = HttpWebRequest.Create(url);
// We have a proxy on the domain, so authentication is required.
WebProxy proxy = new WebProxy("myproxy.mydomain.com", 8080);
proxy.Credentials = new NetworkCredential("username", "password", "mydomain");
rqst.Proxy = proxy;
rqst.Method = "POST";
if (!String.IsNullOrEmpty(postdata))
{
rqst.ContentType = "application/x-www-form-urlencoded";
byte[] byteData = Encoding.UTF8.GetBytes(postdata);
rqst.ContentLength = byteData.Length;
using (Stream postStream = rqst.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
postStream.Close();
}
}
((HttpWebRequest)rqst).KeepAlive = false;
StreamReader rsps = new StreamReader(rqst.GetResponse().GetResponseStream());
string strRsps = rsps.ReadToEnd();
return strRsps;
}
The problem is, when calling GetRequestStream I keep getting a WebException with the message "The remote server returned an error: (502) Bad Gateway."
At first I thought it had to do with the SSL certificate verification. So I added this line:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
Where
public class AcceptAllCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
System.Security.Cryptography.X509Certificate certificate,
WebRequest request,
int certificateProblem)
{
return true;
}
}
And I keep getting the same 502 error. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这种情况发生在我身上,因为如果 Java 应用程序没有及时响应,远程计算机上的 Java 代理就会使请求超时,从而使 .NET 默认超时变得毫无用处。以下代码循环遍历所有异常并写出响应,这帮助我确定它实际上来自代理:
来自代理的响应正文如下所示:
This was happening for me because a Java proxy on the remote machine was timing out requests if the Java application did not respond in time, rendering the .NET default timeouts kind of useless. The following code loops through all exceptions and writes out responses which helped me determine it was actually coming from the proxy:
The response body from the proxy looked something like this:
缺少 UserAgent :
例如
UserAgent is missing
for example :
Web 服务的 wsdl 有可能与域名和 SSL 证书“争论”。 IIS 将使用 IIS 注册的域名(默认情况下是本地域上的计算机名称,不一定是您的 Web 域)自动生成 Web 服务的 WSDL。如果证书域与 SOAP12 地址中的域不匹配,您将收到通信错误。
It is possible that the wsdl for the web service is "arguing" with the domain name and the SSL certificate. IIS will autogenerate a web service's WSDL using the IIS registered domain name (which by default is the machine name on the local domain, not necessarily your web domain). If the certificate domain doesn't match the domain in the SOAP12 Address you will receive communication errors.
读取错误响应的实体主体。它可能暗示正在发生的事情。
执行此操作的代码如下:
应显示错误响应的完整内容。
Read the entity body of the error response. It might have a hint as to what is happening.
The code to do that is as follows:
That should show the full contents of the error response.
借助这我得到了问题的更详细描述:代理返回消息:“无法识别用户代理。”所以我手动设置了。另外,我更改了代码以使用 GlobalProxySelection.GetEmptyWebProxy< /a>(),如此处所述。最终的工作代码如下。
With the help of this I got a more detailed description of the problem: The proxy was returning the message: "The user agent is not recognized." So I set it manually. Also, I changed the code to use GlobalProxySelection.GetEmptyWebProxy(), as described here. The final working code is included below.