1

I am using the below code to send push notifications from my WCF web service

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 = "";
    }
}

It's working fine but sometimes I am getting the exception

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)

I have deployed my web service on azure server.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
Siddhartha
  • 11
  • 2
  • 6
  • Can you properly format your code? It is hard to read at the moment – dat3450 Sep 25 '17 at 05:38
  • try to add ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; before using (var wc = new WebClient()) – jitender Sep 25 '17 at 05:39
  • Dear Jitender thanx for the reply but as i mentioned that this issue is coming some time not regularly, how do i check why its happening only some time not for all request. – Siddhartha Sep 25 '17 at 06:19

1 Answers1

1

You are sending a request to a site that uses SSL/TLS i.e. starts with https. Like you alluded to in your comment, you need to configure your web client to use SSL/TLS. You need to figure out whether the remote server you are trying to reach uses SSL or TLS then use the correct security mode.

You can see this answered in 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;
    }
}
  • I resolve this issue by adding these two lines ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls; But i am not getting why this error coming for some time not for every time we call the web services. – Siddhartha Sep 25 '17 at 07:05