Original: C # uses HttpWebRequest and HttpWebResponse to impersonate a website with a verification code
We often encounter the need to program the simulation login to a website, if the site needs to fill in the verification code of how to simulate login?
This article uses the HttpWebRequest and HttpWebResponse simulations to login to a website with a verification code.
Programming interface is very simple, three textbox input user name, password and verification code, an image control to display from the site request to the verification code picture, there are two buttons, a verification code, a login.
Before writing the program, use the browser developer tool to observe the login page what request, I use FireBug here, the following two figure is in the FireBug network panel cut.
You can see a GET request verification code when you open the login page, as you can see in FireBug:
Above the figure can see this sentence: set-cookie guid=c89eabb62d9d4f35b491a8afd371b4ad; path=/
A cookie is saved on the verification code page requested here.
Then we enter the verification code, click "Login" when there is a POST request, in FireBug can see:
The point here is this sentence:CodeStatus=&bkurl=&companyid=&username=test&password=test&Validate=yyxe
From here we can see the user name, password and how the verification code is submitted.
The following is about the procedure:
1. Request a verification code, display it on the program interface, and save the cookie.
2. Submit your name, password and verification code data for a response.
I am here to be a WPF program, if it is WinForm also similar.
The complete code is as follows:
Cookiecontainer cookies =NULL; stringStrcookies =string. Empty; Private voidBtnchangevalidate_click (Objectsender, RoutedEventArgs e) {getvalidateimage (); } /// <summary> ///get verification codes and cookies/// </summary> Private voidgetvalidateimage () {Cookies=NewCookiecontainer (); stringURL ="http://******/picturetimestamp.asp";//Verification code PageHttpWebRequest request =(HttpWebRequest) webrequest.create (URL); Request. Accept="*/*"; Request. Method="GET"; Request. UserAgent="mozilla/5.0"; Request. Cookiecontainer=NewCookiecontainer ();//staging to a new instanceHttpWebResponse response =(HttpWebResponse) request. GetResponse (); MemoryStream Ms=NULL; using(varstream =Response. GetResponseStream ()) {byte[] buffer=NewByte[response. ContentLength]; intoffset =0, Actuallyread =0; Do{Actuallyread= Stream. Read (buffer, offset, buffer. Length-offset); Offset+=Actuallyread; } while(Actuallyread >0); Ms=NewMemoryStream (buffer); } response. Close (); Cookies= Request. Cookiecontainer;//Save CookiesStrcookies = Request. Cookiecontainer.getcookieheader (Request. RequestUri);//convert cookies into stringsBitmapImage Bi=NewBitmapImage (); Bi. BeginInit (); Bi. Streamsource=(Stream) MS; Bi. EndInit (); Imgvalidate.source=bi; } Private voidBtnlogin_click (Objectsender, RoutedEventArgs e) {Login (); } /// <summary> ///Login/// </summary> /// <returns></returns> Private stringLogin () {HttpWebRequest request=NULL; stringURL ="http://******/loginproc.asp";//Login PageRequest =(HttpWebRequest) webrequest.create (URL); Request. Method="POST"; Request. Accept="*/*;"; Request. UserAgent="mozilla/5.0"; Request. ContentType="application/x-www-form-urlencoded"; Request. AllowAutoRedirect=true; Request. Cookiecontainer=cookies; Request. KeepAlive=true; stringPostData =string. Format ("username={0}&password={1}&validate={2}&isautologin=1&submit=", txtUsername.Text, txtPassword.Text, Txtvalidate.text);//this is done in accordance with the post string found in the previous firebug. byte[] Postdatabyte =Encoding.UTF8.GetBytes (postdata); Request. ContentLength=Postdatabyte. Length; using(Stream stream =request. GetRequestStream ()) {stream. Write (Postdatabyte,0, Postdatabyte. Length); } HttpWebResponse Response=(HttpWebResponse) request. GetResponse (); stringStrwebdata =string. Empty; using(StreamReader reader =NewStreamReader (response. GetResponseStream ())) {Strwebdata=Reader. ReadToEnd (); } returnStrwebdata; }
I hope we can help people who are in need.
Original address: http://www.cnblogs.com/mib23/p/3913016.html
C # using HttpWebRequest and HttpWebResponse to impersonate a website with a verification code