Httpclient simulated login implementation

Source: Internet
Author: User
Tags set cookie

Httpclient simulated login implementation

The simplest way is to customize an httpclient through the obtained cookie. If you are interested, you can understand this article.

Httpclient simulated login (set cookie using js)

The Code is as follows:

<Html>

<Meta http-equiv = 'content-type' Content = 'text/html; charset = gb2312 '>

<Link rel = stylesheet type = text/css href = '/bbs.css'>

<Script> document. cookie = 'utmpnum = 51; path =/; domain =. '+ window. location. host </script>

<Script> document. cookie = 'utmpkey = 20154732; path =/; domain =. '+ window. location. host </script>

<Script> document. cookie = 'utmpuserid = yay; path =/; domain =. '+ window. location. host </script>

<Meta http-equiv = 'refresh' content = '0; url =/firstpage. php'>

 

H4ttpclient 4.3:

 

The simplest way is to customize an httpclient through the cookie. Based on the example above, the method is as follows:

The Code is as follows:

CookieStore cookieStore = new BasicCookieStore ();

 

For (int I = 0; I <3; I ++ ){

String name;

String value;

Int flag = s. indexOf ("document. cookie ");

S = s. substring (flag + 17 );

Flag = s. indexOf ('= ');

Name = s. substring (0, flag );

Value = s. substring (flag + 1, s. indexOf (';'));

BasicClientCookie cookie = new BasicClientCookie (name,

Value );

Cookie. setVersion (0 );

Cookie. setDomain (".www.zju88.org"); // The URL corresponds to <span style = "font-family: Arial, Helvetica, sans-serif;"> window. location. host return value </span>

Cookie. setPath ("/");

CookieStore. addCookie (cookie );

}

// Set the store

CloseableHttpClient httpclient = HttpClients. custom ()

. Setdefacookcookiestore (cookieStore)

. Build ();

 

Android:

 

I haven't found the API corresponding to the above method for a long time. I can only add a cookie before each access.

The Code is as follows:

String cookie = "";

For (int I = 0; I <3; I ++ ){

String name;

String value;

Int flag = s. indexOf ("document. cookie ");

S = s. substring (flag + 17 );

Flag = s. indexOf ('= ');

Name = s. substring (0, flag );

Value = s. substring (flag + 1, s. indexOf (';'));

Cookie + = name + "=" + value;

If (I! = 2)

Cookie + = ";";

}

 

/*

* Network Access

*/

HttpGet httpget = new HttpGet (url );

Httpget. addHeader ("Cookie", cookie );

// If httpclient already has a cookie, you may need to set the httpclient cookie policy. For details, see the official API (:

HttpResponse response = httpclient.exe cute (httpget );

 

 

Related Article

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.