The static void Main (string [] args)
{
StrUserName="test";
StrPassWord="test";
ServicePointManager. SecurityProtocol=SecurityProtocolType. Ssl3 | SecurityProtocolType. Tls | SecurityProtocolType. Tls11 | SecurityProtocolType. Tls12;
ServicePointManager. ServerCertificateValidationCallback=(sender, cert, chain, the error)=& gt;
{
return true;
};
String getJson=string. The Empty;
The string url="https://10.0.18.41:8081/api/v1/oauth/token";
String postdata="https://bbs.csdn.net/topics/grant_type=password&username=" + strUserName + "& amp; Password="+ strPassWord;
GetJson=PostUrl (url, postdata);
Console. WriteLine (getJson);
The Console. ReadKey ();
}
The static string PostUrl (string strUrl, string strPostData)
{
String strReturnValue=https://bbs.csdn.net/topics/string.Empty;
Try
{
HttpWebRequest httpReq=(HttpWebRequest) WebRequest. Create (strUrl);
HttpReq. Method="POST";
HttpReq. Timeout=100000;//set the request timeout milliseconds
HttpReq. KeepAlive=false;
HttpReq. ProtocolVersion=HttpVersion. Version10;
HttpReq. ContentType="application/x - WWW - form - urlencoded";
If (strUserName!=string. The Empty | | strPassWord!=string. The Empty)
{
String usernamePassword=strUserName + ":" + strPassWord;
UsernamePassword="test1: test1";
HttpReq. Headers. The Add (" Authorization ", "Basic" + Convert. ToBase64String (Encoding. UTF8. GetBytes (usernamePassword)));
}
Byte [] bytePostData=https://bbs.csdn.net/topics/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. The method GetResponse ();
The Stream Stream=httpResponse. GetResponseStream ();
//get response content
Using (StreamReader reader=new StreamReader (stream, Encoding UTF8))
{
StrReturnValue=(https://bbs.csdn.net/topics/reader.ReadToEnd);
Reader. The Close ();
}
If (httpResponse!=null)
{
HttpResponse. Close ();
}
If (httpReq!=null)
{
HttpReq. Abort ();
}
}
The catch (Exception ex)
{
AppLogHelper. WriteErrorLog (ex);
}
Return strReturnValue;
}
CodePudding user response:
CodePudding user response:
ServicePointManager. SecurityProtocol=SecurityProtocolType. Ssl3 | SecurityProtocolType. Tls | SecurityProtocolType. Tls11 | SecurityProtocolType. Tls12; This annotation to try, may be system does not support the new soCodePudding user response: