Home > Net >  C # access the json data error: packet format by accident, a handshake failure.
C # access the json data error: packet format by accident, a handshake failure.

Time:01-04


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 so

CodePudding user response:

refer to the second floor qq_15794739 response:
ServicePointManager. SecurityProtocol=SecurityProtocolType. Ssl3 | SecurityProtocolType. Tls | SecurityProtocolType. Tls11 | SecurityProtocolType. Tls12; This annotation to try, have the system does not support this new

I can't, I comment tried once, each also tried separately, all not line

CodePudding user response:

Has been, or a moment appeared, the client and server on one device?

CodePudding user response:

reference 4 floor qq_15794739 response:
has been, or a moment appeared, the client and server on one device?

Is not a device has been written in python language test can retrieve data, but is not even, c # has been submitted to the wrong,
This is the python code
Import HTTP client, urllib. Parse json
The import base64

Api_host1='10.0.18.41:8081
Bearer=base64. B64encode (bytes (' test1: test1 ', 'utf-8))
Retrieve the user token #
Def getAccessToken (username, password) :
Params='grant_type=password& The username='+ username + & amp; Password='+ password
Print (params)
Headers={" cache-control ":" no - Cache ", "X - Requested - With" : "XMLHttpRequest"}
[" the content-type headers "]='application/x - WWW - form - urlencoded'
Headers [" Authorization "]='Basic' + bearer. The decode (' utf-8)
Conn=HTTP. Client. HTTPConnection (api_host1)
Conn. Request (" POST ", "/API/v1/request/token", params, headers)
The response=conn. The method getresponse ()
Print (response. The status and the response. A tiny)
Data=(https://bbs.csdn.net/topics/response.read). Decode (' utf-8)
R=json. Loads (data)
Token=r/' access_token '
Conn. Close ()
Return the token

CodePudding user response:

ServicePointManager. SecurityProtocol=(SecurityProtocolType) 192 | 768 | (SecurityProtocolType) (SecurityProtocolType) 3072;


The 3 kinds of agreement, try, try a single also

CodePudding user response:

It still won't do

refer to 6th floor qq_15794739 response:
ServicePointManager. SecurityProtocol=(SecurityProtocolType) 192 | 768 | (SecurityProtocolType) (SecurityProtocolType) 3072;


The three agreements, try, and a single try
nullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related