2017-09-25 25 views
0

私はそれが正常に動作しますが、時々私のマイWCFのWebサービスからSystem.Net.WebException:要求は中止されました:SSL/TLSのセキュリティで保護されたチャネルを作成できませんでした

using (var wc = new WebClient()) 
{ 
    wc.Headers.Add("Authorization", "Key=" + ConfigurationSettings.AppSettings["apiKey"]); 
    var nameValues = new NameValueCollection 
    { 
     {"registration_id", objSendNotificationBE.RegistrationID}, 
     {"collapse_key", Guid.NewGuid().ToString()}, 
     {"data.payload", objSendNotificationBE.Message} 
    }; 
    var resp = wc.UploadValues("https://android.googleapis.com/gcm/send", nameValues); 
    respMessage = Encoding.Default.GetString(resp); 
    if (respMessage == "Error=InvalidRegistration") 
    { 
     string respMessage = ""; 
    } 
} 

をプッシュ通知を送信するために以下のコードを使用しています例外を取得しています

System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. 
    at System.Net.WebClient.UploadValues(Uri address, String method, NameValueCollection data) 
    at System.Net.WebClient.UploadValues(String address, NameValueCollection data) 

WebサービスをAzureサーバーにデプロイしました。

+0

に答えを見ることができますか?現在読むのは難しい – dat3450

+0

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12を追加しようとすると、 (var wc = new WebClient())を使用する前に – jitender

+0

返信用のJitenderありがとうございましたが、この問題は定期的ではないと言われています。 – Siddhartha

答えて

0

SSL/TLSを使用するサイト(httpsで始まる)にリクエストを送信しています。あなたのコメントでお伝えしたとおり、SSL/TLSを使用するようにWebクライアントを設定する必要があります。到達しようとしているリモートサーバーがSSLまたはTLSを使用しているかどうかを確認し、正しいセキュリティモードを使用する必要があります。

あなたは、これはあなたが適切にあなたのコードをフォーマットすることができthis question.

using System.Net; 
using System.Net.Security; 
using System.Security.Cryptography.X509Certificates; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Uri address = new Uri("https://archive.org/details/OTRR_In_The_Name_Of_The_Law_Singles"); 

     ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 ; 

     using (WebClient webClient = new WebClient()) 
     { 
      var stream = webClient.OpenRead(address); 
      using (StreamReader sr =new StreamReader(stream)) 
      { 
       var page = sr.ReadToEnd(); 
      } 
     } 
    } 

    /// <summary> 
    /// Certificate validation callback. 
    /// </summary> 
    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) 
    { 
     // If the certificate is a valid, signed certificate, return true. 
     if (error == System.Net.Security.SslPolicyErrors.None) 
     { 
      return true; 
     } 

     Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'", 
      cert.Subject, 
      error.ToString()); 

     return false; 
    } 
} 
+0

私はServicePointManager.Expect100Continue = trueという2つの行を追加してこの問題を解決しました。 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;しかし、なぜ私たちがWebサービスを呼び出すたびに、このエラーがいつ来るのか分かりません。 – Siddhartha

関連する問題

 関連する問題