3

適切な 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# で動作させる方法はありますか? ありがとう!

4

1 に答える 1

2

さらに検索した結果、同様の問題を抱えていて解決策を見つけた人を見つけました。このような状況をデバッグするには、ここで説明されているように SSL トレース/ログを有効にします。http://blogs.msdn.com/b/dgorti/archive/2005/09/18/471003.aspx

私の問題は、次のようにSSL3接続を宣言する必要があることでした

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

呼び出す前にこれをコードに追加するとclient.UploadData、問題が修正されました。

于 2013-09-13T21:06:46.270 に答える