エラー502(不正なゲートウェイ)SSL上では、HttpWebRequestでリクエストを送信します
-
23-09-2019 - |
質問
私は、コマンドを送信し、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
解決 2
付きこのののヘルプには、私は、問題の詳細な説明を得た:プロキシは、メッセージを返していました:「のユーザエージェントは、は認識されていません。」だから私はそれを手動で設定します。また、私が使用するコードを変更 GlobalProxySelection.GetEmptyWebProxy >()、のここで記載されているように。最終の作業コードを以下に含まれている。
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;
}
他のヒント
エラー応答のエンティティボディをお読みください。これは、何が起こっているのかについてのヒントを持っているかもしれません。
次のようにそれを行うためのコードがあります:
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)";
これは無用の.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 /xxx/xxx/xxx</a></em>.<p>
Reason: <strong>Error reading from remote server</strong></p></p>
</body></html>