發送請求時,我有在傳統的ASP下面的代碼段,以發送命令和檢索通過SSL的響應:錯誤502(錯誤網關)與HttpWebRequest的通過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
然後我用this code作爲參考重新實現在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;
}
問題是,調用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錯誤。有任何想法嗎?
優秀的諮詢!使用這個我發現了一些關於這個問題的更多信息,並提出了一個解決方案。謝謝。 – Leonardo 2010-02-01 04:31:17