2010-01-29 138 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>