tengo el siguiente fragmento de código en ASP clásico, para enviar un comando y recuperar la respuesta a través de SSL:Error 502 (Bad Gateway) al enviar una solicitud con HttpWebRequest sobre 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
Luego usé this code como reimplementar referencia a la solicitud en 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;
}
el problema es que cuando se llama a GetRequestStream me siguen dando un WebException con el mensaje "The remote server returned an error: (502) Bad Gateway."
Al principio pensé que tenía que ver con la verificación del certificado SSL. Por lo que añade esta línea:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
Dónde
public class AcceptAllCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
System.Security.Cryptography.X509Certificate certificate,
WebRequest request,
int certificateProblem)
{
return true;
}
}
Y sigo obteniendo el mismo error 502. ¿Algunas ideas?
Excelente consejo! Usando esto descubrí un poco más sobre el problema y se me ocurrió una solución. Gracias. – Leonardo