Подтвердить что ты не робот

Ошибка 502 (Bad Gateway) при отправке запроса с помощью HttpWebRequest через SSL

У меня есть следующий фрагмент в классическом 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

Затем я использовал этот код в качестве ссылки для повторной реализации запроса в С#:

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. Любые идеи?

4b9b3361

Ответ 1

С помощью этого я получил более подробное описание проблемы: прокси-сервер возвращал сообщение: " Агент пользователя не распознается". Поэтому я установил его вручную. Кроме того, я изменил код, чтобы использовать 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;
}

Ответ 2

Прочитайте тело объекта ответа об ошибке. Возможно, у него есть намек на то, что происходит.

Код для этого выглядит следующим образом:

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

Это должно показать полное содержимое ответа об ошибке.

Ответ 3

Возможно, что wsdl для веб-службы "спорит" с именем домена и сертификатом SSL. IIS автоматически создаст WSDL веб-службы, используя зарегистрированное доменное имя IIS (которое по умолчанию является именем компьютера в локальном домене, не обязательно вашим веб-доменом). Если домен сертификата не соответствует домену в адресе SOAP12, вы получите ошибки связи.

Ответ 4

Отсутствует UserAgent

например: request.UserAgent = "Mozilla/4.0 (совместимо; MSIE 7.0; Windows NT 5.1)";

Ответ 5

Это происходило для меня, потому что прокси-сервер 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>