Error 502 (Bad Gateway) when sending a request with HttpWebRequest over SSL

前端 未结 5 736
误落风尘
误落风尘 2020-12-13 14:08

I have the following snippet in classic ASP, to send a command and retrieve the response over SSL:

Dim xmlHTTP
Set xmlHTTP = Server.CreateObject(\"Msxml2.Ser         


        
5条回答
  •  眼角桃花
    2020-12-13 15:01

    With the help of this I got a more detailed description of the problem: The proxy was returning the message: "The user agent is not recognized." So I set it manually. Also, I changed the code to use GlobalProxySelection.GetEmptyWebProxy(), as described here. The final working code is included below.

    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;
    }
    

提交回复
热议问题