適切な SSL 証明書がインストールされていないサーバー (テスト開発サーバー) に対して、C# で HTTPS POST を実行しようとしています。要求は、WebClient と HttpWebRequest を使用してタイムアウトしています。証明書チェックをバイパスするように独自の ServerCertificateValidationCallback をセットアップしましたが、それは役に立ちませんでした。Web ページでまったく同じ呼び出しを行うと、その呼び出しは成功します。
そう:
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# で動作させる方法はありますか? ありがとう!