static void Main(string[] args)
        {
            strUserName = "test";
            strPassWord = "test";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) =>
            {
                return true;
            };
            string getJson = string.Empty;
            string url = "https://10.0.18.41:8081/api/v1/oauth/token";
            string postdata = "grant_type=password&username=" + strUserName + "&password=" + strPassWord;
            getJson = PostUrl(url, postdata);
            Console.WriteLine(getJson);
            Console.ReadKey();
        }        static string PostUrl(string strUrl, string strPostData)
        {
            string strReturnValue = string.Empty;
            try
            {               
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(strUrl);
                httpReq.Method = "POST";
                httpReq.Timeout = 100000;//设置请求超时时间,单位为毫秒
                httpReq.KeepAlive = false;
                httpReq.ProtocolVersion = HttpVersion.Version10;
                httpReq.ContentType = "application/x-www-form-urlencoded";
                if (strUserName != string.Empty || strPassWord != string.Empty)
                {
                    string usernamePassword = strUserName + ":" + strPassWord;
                    usernamePassword = "test1:test1";
                    httpReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(usernamePassword)));
                }
                byte[] bytePostData = Encoding.UTF8.GetBytes(strPostData);
                httpReq.ContentLength = bytePostData.Length;
                using (Stream reqStream = httpReq.GetRequestStream())
                {
                    reqStream.Write(bytePostData, 0, bytePostData.Length);
                    reqStream.Close();
                }
                HttpWebResponse httpResponse = (HttpWebResponse)httpReq.GetResponse();
                Stream stream = httpResponse.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    strReturnValue = reader.ReadToEnd();
                    reader.Close();
                }
                if (httpResponse != null)
                {
                    httpResponse.Close();
                }
                if (httpReq != null)
                {
                    httpReq.Abort();
                }              
            }
            catch(Exception ex)
            {
                AppLogHelper.WriteErrorLog(ex);
            }
            return strReturnValue;
        }

解决方案 »

  1.   

       ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;  这个注释试一下,有可系统不支持这么新的
      

  2.   

    一直出现,不在一个设备上,用python语言写的测试可以获取数据的,但是C#就是连不上,一直报这个错。
    这是python的代码
    import http.client, urllib.parse, json
    import base64api_host1='10.0.18.41:8081'
    bearer = base64.b64encode(bytes('test1:test1','utf-8'))
    #获取用户token
    def getAccessToken(username, password):
      params = 'grant_type=password&username=' + username + '&password=' + password
      print(params)
      headers = {"Cache-Control": "no-cache", "X-Requested-With":"XMLHttpRequest" }
      headers["Content-Type"] = 'application/x-www-form-urlencoded'
      headers["Authorization"] = 'Basic ' + bearer.decode('utf-8')
      conn = http.client.HTTPConnection(api_host1)
      conn.request("POST", "/api/v1/oauth/token", params, headers)
      response = conn.getresponse()
      print(response.status, response.reason)
      data = response.read().decode('utf-8')
      r = json.loads(data)
      token = r['access_token']
      conn.close()
      return token
      

  3.   

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    这3种协议,试一下,单个也试一下
      

  4.   

    协议原因,https应该改成http,不知道为什么python上面就可以通用,兜兜转转最后回到原点,python这点是真的坑啊。