将httpwebrequest发布到渠道顾问

时间:2018-07-19 09:08:10

标签: c# channeladvisor

我正在尝试从channeladvisor电子商务获取新令牌 我正在使用此代码

       // 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;
        }

但我不断收到错误消息

System.Net.WebException:请求已中止:无法创建SSL / TLS安全通道。 有什么问题谢谢

1 个答案:

答案 0 :(得分:0)

尝试此添加此行:-

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

Refer More From Here which help you alot