Open the logged-on webpage on the client (C #))

Source: Internet
Author: User

Instant messaging software such as QQ and Baidu Hi have such functions. You click some buttons on the client, and the Web pages opened are all logged on, because the client has logged on, you do not need to log on again on the webpage.

Today, I learned about this problem from a netizen on Baidu, so I took the time to study it and help solve it.

 

It is also relatively easy to implement with C # (but there is a condition that no verification code is available during webpage login), that is, to log in with httpwebrequest to obtain the cookie value, then, write the cookie to the cookie directory of the browser, and then open the browser. The following is the implementationCodeIf you have any questions, please leave a message.

 

Code

Using System. IO;

UsingSystem. text;

UsingSystem. net;

UsingSystem. runtime. interopservices;

ClassLogin

{

[dllimport ( " wininet. DLL " , charset = charset. auto, setlasterror = true )]

Public static extern bool internetsetcookie ( string lpszurlname, string lbszcookiename, string lpszcookiedata );

Public Static VoidMain ()

{

Cookiecollection mycookies =   New Cookiecollection (); // Cookie set

Byte [] Data = Encoding. ASCII. getbytes ( " Username = xxxxxx & Password = ******** " ); // Username and password information

String URL =   @" Http://www.xxx.com /...... " ; // Action address of the login form

Httpwebrequest myrequest = (Httpwebrequest) webrequest. Create (URL );

Myrequest. Method= "Post";

Myrequest. contenttype= "Application/X-WWW-form-urlencoded";

Myrequest. contentlength=Data. length;

Myrequest. cookiecontainer= NewCookiecontainer ();

Try 

{

Stream newstream=Myrequest. getrequeststream ();

Newstream. Write (data,0, Data. Length );

Newstream. Close ();

}

Catch(Webexception)

{

Throw NewWebexception ("Network Connection error!");

}

Httpwebresponse myresponse=(Httpwebresponse) myrequest. getresponse ();

Mycookies. Add (myresponse. Cookies );//Add cookie

Foreach(CookieInMycookies)//Set a cookie as a browser cookie

{

Internetsetcookie (

"Http ://" +Cookie. domain. tostring (),

Cookie. Name. tostring (),

Cookie. value. tostring ()+ "; Expires = sun, fig 00:00:00 GMT");

}

System. Diagnostics. process. Start ("Http://www.XXX.com/");//Open a browser

}

}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.