0

I am trying to get new token from channeladvisor ecommerce I am using this code

       // HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.channeladvisor.com/oauth2/token");
       var request=HttpWebRequest.Create("https://api.channeladvisor.com/oauth2/token");
        WebResponse response = null;
        string responseString = string.Empty;
        try
        {
        //    request.Timeout = 300000;
          //  request.KeepAlive = false;
          //request.ContentType = "application/json";                
            request.UseDefaultCredentials = true;
            string credentials = applicationid + ":" + sharesecret;
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(credentials);
            string base64 = Convert.ToBase64String(bytes);
            request.Headers["Authorization"] = "Basic " + base64;
            request.ContentType = " application/x-www-form-urlencoded";

              ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 |
                                    SecurityProtocolType.Tls12;

            request.Method = "Post";
           // var stringContent = new StringContent("grant_type=refresh_token&refresh_token=" + refreshToken);
            request.ContentLength = System.Text.Encoding.ASCII.GetByteCount("grant_type=refresh_token&refresh_token=" + refreshToken);
            byte[] buffer = System.Text.Encoding.ASCII.GetBytes("grant_type=refresh_token&refresh_token=" + refreshToken);
           // string result = System.Convert.ToBase64String(buffer);
            Stream reqstr = request.GetRequestStream();
            reqstr.Write(buffer, 0, buffer.Length);
            reqstr.Close();
            response = (HttpWebResponse)request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream);
                responseString = reader.ReadToEnd();
            }
        }
        catch (Exception)
        {

            throw;
        }

but I keep getting the error

System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. what is problem? thank you

Khaled
  • 1
  • 1

1 Answers1

0

Try this Added this line:-

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Use SecurityProtocolType.Ssl3 if needed for compatibility reasons

Refer More From Here which help you alot

Hitesh Anshani
  • 1,499
  • 9
  • 19
  • thank you I solved it by write ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; before var request=HttpWebRequest.Create("https://api.channeladvisor.com/oauth2/token"); – Khaled Jul 19 '18 at 12:23
  • Ohk.great khaled – Hitesh Anshani Jul 19 '18 at 12:46