我正在尝试在 C# 中对未安装正确 SSL 证书的服务器(我的测试开发服务器)进行 HTTPS POST。该请求使用 WebClient 和 HttpWebRequest 超时。我已经设置了自己的 ServerCertificateValidationCallback 来绕过证书检查,但这并没有帮助。如果我在网页中进行完全相同的调用,则该调用成功。
所以:
Call to URL https://testServer/myAction?myData in webpage - succeeds.
POST to https://testServer/myAction with myData using WebClient - timeout.
我的 WebClient 帖子代码如下:
private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
//solve the problem of invalid certificates - accept all as valid
return true;
}
public void callPost(object o)
{
string myData = (string)o;
try
{
Uri uri = new Uri("https://testServer/myAction");
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
byte[] uploadBytes = Encoding.UTF8.GetBytes(myData);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
byte[] responseBytes = client.UploadData(uri, "POST", Encoding.UTF8.GetBytes(urlData));
Console.WriteLine("WebRequest:{0}\n", Encoding.ASCII.GetString(responseBytes));
}
catch (Exception e)
{
Console.WriteLine("WebRequest Error:{0}\n", e.ToString());
}
}
任何想法如何让 POST 在 C# 中工作?谢谢!