The download of many websites involves user verification, so that the convenient WebClient. Download Method in the past cannot request the target data. Httpwebrequest. getresponse is not supported.
Here, two available verification downloads are provided for the two components. The idea is to use webbrowser to obtain cookie information and construct a valid cookie to pass it to the two components to provide download permissions.
The implementation of httpwebrequest is relatively easy, because httpwebrequest itself supports setting cookiecontainer information. Therefore, the first implementation is as follows:
Cookiecontainer mycookiecontainer = new cookiecontainer ();
// Construct our cookiecontainer here
String cookiestr = webbrowser1.document. Cookie;
String [] cookstr = cookiestr. Split (';');
Foreach (string STR in cookstr)
{
String [] cookienamevalue = Str. Split ('= ');
Cookie ckk = new cookie (cookienamevalue [0]. Trim (). tostring (), cookienamevalue [1]. Trim (). tostring ());
Ckk. Domain = "www.xxx.com"; // required
Mycookiecontainer. Add (ckk );
}
// Send the constructed cookie to the httpwebrequest object and construct the output stream to read the download information.
String actionurl = "http: // 10.1.1.78/file. php/12/esd1005/20100510am/000.asf ";
Httpwebrequest httprequest = (httpwebrequest) webrequest. Create (actionurl );
// Pass cookiecontainer
Httprequest. cookiecontainer = mycookiecontainer;
Httpwebresponse response = (httpwebresponse) httprequest. getresponse ();
//// Put the response in stream
Stream responsestream = response. getresponsestream ();
Streamreader responsereader = new streamreader (responsestream );
In this way, the output stream returned by the website is obtained and put into streamreader. Everyone knows how to deal with it.
The following describes the WebClient object. After careful research, this object is a bit interesting. It does not save its original cookie every time it initiates a request, so it cannot set the cookiecontainer attribute, this is annoying. Later, I saw a more interesting thing in csdn. When sending a request, it used the getwebrequest method to return the webrequest object. What does it mean? If the webrequest obtained by this method is httpwebrequest, we can add the cookiecontainer object to this method. Further, we can rewrite the method to keep the cookie information.
The rewrite code is as follows:
Public class httpclient: WebClient
{
// Cookie container
Private cookiecontainer;
/**/
/// <Summary>
/// Create a new WebClient instance.
/// </Summary>
Public httpclient ()
{
This. cookiecontainer = new cookiecontainer ();
}
/**/
/// <Summary>
/// Create a new WebClient instance.
/// </Summary>
/// <Param name = "cookie"> cookie container </param>
Public httpclient (cookiecontainer cookies)
{
This. cookiecontainer = cookies;
}
/**/
/// <Summary>
/// Cookie container
/// </Summary>
Public cookiecontainer cookies
{
Get {return this. cookiecontainer ;}
Set {This. cookiecontainer = value ;}
}
/**/
/// <Summary>
/// Return httpwebrequest with Cookie.
/// </Summary>
/// <Param name = "Address"> </param>
/// <Returns> </returns>
Protected override webrequest getwebrequest (URI address)
{
Webrequest request = base. getwebrequest (Address );
If (request is httpwebrequest)
{
Httpwebrequest httprequest = request as httpwebrequest;
Httprequest. cookiecontainer = cookiecontainer;
}
Return request;
}
}
In this way, we can directly reference
The httpclient object uses WebClient. It is not difficult to see that httpclient inherits webcilent and adds the constructor Number of cookie initialization. What does it mean? We can directly construct the httpclient object and pass the obtained cookiecontainer to this object. Then you can get the download permission verified by the user.
In fact, the reason is very simple. Some people say they can use the add method of WebClient header to add cookies, but ye Xuan thinks that is still inappropriate, in the test, adding the Cookie field directly to the header of the original WebClient object is invalid because the cookie is reset when the request is sent...
Some of the things are messy. They are all the problems and solutions encountered by Ye Hyun himself in writing the program. If they are helpful to you, ye Xuan will be honored, if it is wrong or inappropriate, please point it out for discussion ~
Author: ye Xuan
Original yixuan article, reproduced any, but please indicate the source ~~
Original address: http://hi.baidu.com/rabby163/blog/item/7e7008507601297184352444.html