Account no problem, when high frequency low
Tried to set the
Request. Credentials=CredentialCache. DefaultCredentials;
Request. UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; The.net CLR 2.0.50727) ";
Certification:
//add the HTTPS
If (the EndPoint. The Substring (0, 8)=="https://")
{
//set the certificate to accept state
ServicePointManager. ServerCertificateValidationCallback=new RemoteCertificateValidationCallback (CheckValidationResult);
}
//certificates set
Private static bool CheckValidationResult (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; Always accept//
}
Interface is a Rest API
//set the Tls security agreement: 192 Tls11:768 Tls12:3072
ServicePointManager. SecurityProtocol=(SecurityProtocolType) 192 | 768 | (SecurityProtocolType) (SecurityProtocolType) 3072;
Var request=(HttpWebRequest) WebRequest. Create (the EndPoint + parameters);
Request. The Method=Method. The ToString ();
Request. The ContentLength=0;
Request. ContentType=ContentType;
Request. Headers. The Add (" Authorization ", strAuthorization);
if (! String. IsNullOrEmpty (PostData) & amp; & Method==HttpVerbNew. POST)//if the transfer of data is not empty, and the Method is POST
{
Var bytes=Encoding. GetEncoding (" utf-8 "). GetBytes (PostData);//encoding according to their own needs to change, I in the project using the utf-8
Request. The ContentLength=bytes. The Length;
Using (var writeStream=request. GetRequestStream ())
{
WriteStream. Write (0 bytes, bytes. The Length).
}
}
if (! String. IsNullOrEmpty (PostData) & amp; & Method==HttpVerbNew. PUT)//if the transfer of data is not empty, and the Method is to PUT the
{
Var bytes=Encoding. GetEncoding (" utf-8 "). GetBytes (PostData);//encoding according to their own needs to change, I in the project using the utf-8
Request. The ContentLength=bytes. The Length;
Using (var writeStream=request. GetRequestStream ())
{
WriteStream. Write (0 bytes, bytes. The Length).
}
}
Using (var response=(HttpWebResponse) request. The method GetResponse ())
{
Var responseValue=https://bbs.csdn.net/topics/string.Empty;
If (the response StatusCode!=HttpStatusCode. OK)
{
Var message=the String. Format (" Request failed. The Received HTTP {0} ", the response. The StatusCode);
Throw new ApplicationException (message);
}
//returns the
Using (var responseStream=response. GetResponseStream ())
{
If (responseStream!=null)
Using (var reader=new StreamReader (responseStream))
{
ResponseValue=(https://bbs.csdn.net/topics/reader.ReadToEnd);
}
}
Return responseValue;
CodePudding user response:
Generally do not own splicing Authorization specific string,Reason is that when the server 401 for authorization challenge, can use different solutions, as well as the different parameters,
Such as scheme is the Basic/Digest/Negotiate and so on, even if you use the same user name password, under different solution/parameters, Authorization string is not the same as a result,
Usually approach is to set the request Credentials, let HttpWebRequest to adaptive,
CodePudding user response: