Home > Net >  About WebClient derived class and assign a Cookie?
About WebClient derived class and assign a Cookie?

Time:01-29

Got a piece of code on the net, have a question, to solve, the following code
 
Public class WebClientHelper: WebClient
{
Public WebClientHelper () : this (new CookieContainer ())
{

}

Public WebClientHelper (CookieContainer c)
{
Enclosing CookieContainer=c;
}

Public CookieContainer CookieContainer {get; set; }


Protected override WebRequest GetWebRequest address (Uri)
{
WebRequest request=base. GetWebRequest (address);
If (request is HttpWebRequest)
{
(the request as HttpWebRequest). CookieContainer=this. CookieContainer;
}
Return the request;
}
///& lt; Summary>
///the url redirection after
///& lt;/summary>
Public string ServerUrl {get; set; }
Protected override WebResponse GetWebResponse (WebRequest request)
{
Var response=base. GetWebResponse (request);//when code execution here cookies have value, how to assign a value to a Cookie?
ServerUrl=response. ResponseUri. Authority;
Return to base. GetWebResponse (request);
}



Var response=base. GetWebResponse (request); When carried out this code cookies have value, how to assign a value to a Cookie

CodePudding user response:

GetWebResponse server will give the client the need of cookies, convenient authentication and so on.
If you want to use the crawler, or unconventional way access to the server resources, the need to to set the cookie, bypass part of the operation, should be set before you send to specify request.
You have to understand this process

CodePudding user response:

reference 1st floor ziqi0716 response:
GetWebResponse server will give the client the need of cookies, authentication, etc.
If you want to use the crawler, or unconventional way access to the server resources, the need to to set the cookie, bypass part of the operation, should be set before you send to specify request.
The process you have to understand that


Occasionally use the crawler, don't quite understand, I just want to know how this value is assigned to the CookieContainer members

CodePudding user response:

This is to get the server response of Cookie, will automatically generate a CookieContainer, normal process is after get response content, the CookieContainer preserved, assigned to the WebRequest a bit longer.
  •  Tags:  
  • C#
  • Related