エラー502(不正なゲートウェイ)SSL上では、HttpWebRequestでリクエストを送信します

StackOverflow https://stackoverflow.com/questions/2159361

質問

私は、コマンドを送信し、SSL経由の応答を取得するために、従来のASPに次のコードを持っています:

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
それから私は、このコードをhref="https://stackoverflow.com/questions/773638/posting-using-post-from-c-over-https/773682#773682"> :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を呼び出すときに

問題は、私がメッセージ"The remote server returned an error: (502) Bad Gateway."とWebExceptionを得続けるさ。

最初のIで

は、それが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エラーを得続けます。任意のアイデア?

他のヒント

エラー応答のエンティティボディをお読みください。これは、何が起こっているのかについてのヒントを持っているかもしれません。

次のようにそれを行うためのコードがあります:

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

これはエラー応答の完全な内容を表示する必要があります。

WebサービスのWSDLは、ドメイン名とSSL証明書で、「主張」することも可能です。 IISは、(デフォルトではローカルドメイン上のマシン名で、必ずしもあなたのWebドメイン)IIS登録されたドメイン名を使用してWebサービスのWSDLを自動生成します。証明書のドメインがSOAP12アドレスのドメインと一致しない場合は、通信エラーを受信します。

のUserAgentが欠落している。

例えば: request.UserAgent = "Mozillaの/ 4.0(互換; MSIE 7.0; Windows NTの5.1)";

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>
scroll top