Ich habe folgende Code-Schnipsel in klassischen ASP, einen Befehl zu senden und die Antwort über SSL abrufen:Fehler 502 (Bad Gateway), wenn das Senden einer Anfrage mit HttpWebRequest über 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
Dann habe ich this code als Referenz die Anfrage in C# neu zu implementieren:
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;
}
Das Problem ist, wenn GetRequestStream Aufruf ich erhalte eine WebException mit der Meldung "The remote server returned an error: (502) Bad Gateway."
Zuerst dachte ich, es hätte mit der Verifizierung des SSL-Zertifikats zu tun. Also hat ich diese Zeile:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
Wo
public class AcceptAllCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
System.Security.Cryptography.X509Certificate certificate,
WebRequest request,
int certificateProblem)
{
return true;
}
}
Und ich halte die gleichen 502 Fehler. Irgendwelche Ideen?
Exzellente Beratung! Damit habe ich ein wenig mehr über das Problem herausgefunden und eine Lösung gefunden. Vielen Dank. – Leonardo