2010-01-29 192 views
22

发送请求时,我有在传统的ASP下面的代码段,以发送命令和检索通过SSL的响应:错误502(错误网关)与HttpWebRequest的通过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 

然后我用this code作为参考重新实现在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与消息"The remote server returned an error: (502) Bad Gateway."

起初我以为它必须处理SSL证书验证。所以我说这行:

ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy(); 

public class AcceptAllCertificatePolicy : ICertificatePolicy 
{ 
    public bool CheckValidationResult(ServicePoint srvPoint, 
             System.Security.Cryptography.X509Certificate certificate, 
             WebRequest request, 
             int certificateProblem) 
    { 
     return true; 
    } 
} 

而且我不断收到同样的502错误。有任何想法吗?

回答

26

With the help of this我得到了更详细的问题描述:代理返回消息:“用户代理不被识别。”所以我手动设置它。另外,我将代码更改为使用GlobalProxySelection.GetEmptyWebProxy(),如here所述。最终的工作代码如下。

private static string SendRequest(string url, string postdata) 
{ 
    if (String.IsNullOrEmpty(postdata)) 
     return null; 
    HttpWebRequest rqst = (HttpWebRequest)HttpWebRequest.Create(url); 
    // No proxy details are required in the code. 
    rqst.Proxy = GlobalProxySelection.GetEmptyWebProxy(); 
    rqst.Method = "POST"; 
    rqst.ContentType = "application/x-www-form-urlencoded"; 
    // In order to solve the problem with the proxy not recognising the user 
    // agent, a default value is provided here. 
    rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; 
    byte[] byteData = Encoding.UTF8.GetBytes(postdata); 
    rqst.ContentLength = byteData.Length; 

    using (Stream postStream = rqst.GetRequestStream()) 
    { 
     postStream.Write(byteData, 0, byteData.Length); 
     postStream.Close(); 
    } 
    StreamReader rsps = new StreamReader(rqst.GetResponse().GetResponseStream()); 
    string strRsps = rsps.ReadToEnd(); 
    return strRsps; 
} 
1

Web服务的wsdl有可能与域名和SSL证书“争论”。 IIS将使用IIS注册域名(默认为本地域上的机器名称,不一定是您的Web域)自动生成Web服务的WSDL。如果证书域与SOAP12地址中的域不匹配,您将收到通信错误。

28

读取错误响应的实体主体。它可能会暗示正在发生的事情。

的代码做如下:

catch(WebException e) 
{ 
if (e.Status == WebExceptionStatus.ProtocolError) 
{ 
    WebResponse resp = e.Response; 
    using(StreamReader sr = new StreamReader(resp.GetResponseStream())) 
    { 
     Response.Write(sr.ReadToEnd()); 
    } 
} 
} 

这应该显示错误响应的全部内容。

+0

优秀的咨询!使用这个我发现了一些关于这个问题的更多信息,并提出了一个解决方案。谢谢。 – Leonardo 2010-02-01 04:31:17

1

的UserAgent缺少

例如: request.UserAgent = “的Mozilla/4.0(兼容; MSIE 7.0; Windows NT的5.1)”;

0

对我而言,这是发生的,因为如果Java应用程序没有及时响应,远程计算机上的Java代理超时请求,从而导致.NET默认超时类型无用。下面的代码遍历所有的异常,并写出反应这让我确定它实际上是从代理来:

static void WriteUnderlyingResponse(Exception exception) 
{ 
    do 
    { 
     if (exception.GetType() == typeof(WebException)) 
     { 
      var webException = (WebException)exception; 
      using (var writer = new StreamReader(webException.Response.GetResponseStream())) 
       Console.WriteLine(writer.ReadToEnd()); 
     } 
     exception = exception?.InnerException; 
    } 
    while (exception != null); 
} 

来自代理的响应体看起来是这样的:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>502 Proxy Error</title> 
</head><body> 
<h1>Proxy Error</h1> 
<p>The proxy server received an invalid 
response from an upstream server.<br /> 
The proxy server could not handle the request <em><a href="/xxx/xxx/xxx">POST&nbsp;/xxx/xxx/xxx</a></em>.<p> 
Reason: <strong>Error reading from remote server</strong></p></p> 
</body></html>