
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;
}
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 這個注釋試一下,有可系統不支持這么新的uj5u.com熱心網友回復:
不行啊,我注釋試了一下,單獨每個也都試了,都不行
uj5u.com熱心網友回復:
一直出現,還是一會一出現,客戶端跟服務端在一臺設備上嗎?uj5u.com熱心網友回復:
一直出現,不在一個設備上,用python語言寫的測驗可以獲取資料的,但是C#就是連不上,一直報這個錯。
這是python的代碼
import http.client, urllib.parse, json
import base64
api_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
uj5u.com熱心網友回復:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;這3種協議,試一下,單個也試一下
uj5u.com熱心網友回復:
這樣還是不行uj5u.com熱心網友回復:
協議原因,https應該改成http,不知道為什么python上面就可以通用,兜兜轉轉最后回到原點,python這點是真的坑啊。uj5u.com熱心網友回復:
協議原因,https應該改成http。C#中https和http是不可以通用的,需要單獨設定,如果協議用錯了額,就容易出現這個錯誤!轉載請註明出處,本文鏈接:https://www.uj5u.com/net/244087.html
標籤:C#
