Leo*_*rdo 24 c# ssl httpwebrequest system.net.webexception
我在经典ASP中有以下代码段,用于发送命令并通过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
Run Code Online (Sandbox Code Playgroud)
然后我使用此代码作为参考,在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;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当调用GetRequestStream时,我不断收到带有消息的WebException "The remote server returned an error: (502) Bad Gateway."
起初我认为它与SSL证书验证有关.所以我添加了这一行:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
Run Code Online (Sandbox Code Playgroud)
哪里
public class AcceptAllCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
System.Security.Cryptography.X509Certificate certificate,
WebRequest request,
int certificateProblem)
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我一直得到同样的502错误.有任何想法吗?
fer*_*oze 32
阅读错误响应的实体主体.它可能暗示正在发生的事情.
执行此操作的代码如下:
catch(WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
WebResponse resp = e.Response;
using(StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
Response.Write(sr.ReadToEnd());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该显示错误响应的全部内容.
Leo*_*rdo 26
在此帮助下,我得到了一个更详细的问题描述:代理正在返回消息:" 用户代理无法识别." 所以我手动设置它.另外,我改变了代码,使用GlobalProxySelection.GetEmptyWebProxy(),如所描述这里.最终的工作代码包含在下面.
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;
}
Run Code Online (Sandbox Code Playgroud)