问题描述
我有下面的代码片断在传统的ASP,发送命令和检索通过SSL响应:
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.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
然后我使用作为参考来重新实现请求在C#中:
Then I used this code as a reference to reimplement the request in 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与消息远程服务器返回错误:(502)错误的网关
起初我还以为是不得不做与SSL证书验证。所以我说这行:
At first I thought it had to do with the SSL certificate verification. So I added this line:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
其中,
Where
public class AcceptAllCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
System.Security.Cryptography.X509Certificate certificate,
WebRequest request,
int certificateProblem)
{
return true;
}
}
和我不断收到同样的502错误。任何想法?
And I keep getting the same 502 error. Any ideas?
推荐答案
的我得到这个问题的更详细的说明的帮助:代理返航的消息: 用户代理无法识别。所以,我手动设置。另外,我改为使用()的代码,描述。最后的工作代码包含下面。
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;
}
这篇关于错误502(错误网关)与HttpWebRequest的通过SSL发送请求时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!