Home > Net >  HttpWebRequest (401) unauthorized problems from time to time
HttpWebRequest (401) unauthorized problems from time to time

Time:09-22

Has set the Headers but sometimes can appear a 401 error,
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:

reference 1st floor github_36000833 response:
is generally 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,


Brother, is authorized the use of the Basic64 encryption (". Basic base64 encode (account: PWD) "), use the postman did not appear this problem,
And does not appear in the test environment call 401 error, when using this problem in formal environment
  • Related