Example of cookie usage when Android interacts with Web server

Source: Internet
Author: User
Tags trim

Here is the specific code
/**

* Send a GET request to the website, the URL should be written according to API requirements, return the information obtained.

*//This is dedicated to the public comment on incoming cookie parameters for the purpose of obtaining the city information chosen by the user

* @param URL

* @param Client

* @return String

* @author Lvqiyong

*/

The code is as follows Copy Code

public static string GetRequest1 (string URL, defaulthttpclient client,

String CharSet) throws Exception {

String result = null;

int statusCode = 0;

HttpGet GetMethod = new HttpGet (URL);

LOG.D (TAG, "do the getrequest,url=" + URL + "");

try {

Getmethod.setheader ("User-agent", user_agent);

Getmethod.setheader ("Cookie", "cy=" + value);//This is dedicated to the public comment on the incoming Cookie parameter for the purpose of obtaining the city information selected by the user

Add User password Authentication information

Client.getcredentialsprovider (). SetCredentials (

New Authscope (NULL,-1),

New Usernamepasswordcredentials (Musername, Mpassword));

HttpResponse HttpResponse = Client.execute (GetMethod);

StatusCode = 200 Normal

StatusCode = Httpresponse.getstatusline (). Getstatuscode ();

LOG.D (TAG, "statuscode =" + statuscode);

Processing the returned HttpResponse information

if (StatusCode = = 200) {

result = Retrieveinputstream (Httpresponse.getentity (), CharSet);

Cookie Cookie;

String Cookname,cookvalue;

list<cookie> cookies = Client.getcookiestore (). GetCookies ();

if (Cookies.isempty ()) {

LOG.I (TAG, "-------Cookie NONE---------");

} else {

for (int i = 0; i < cookies.size (); i++) {

Save cookies

Cookie = Cookies.get (i);

Cookname = Cookie.getname (). Trim ();

Cookvalue = Cookie.getvalue (). Trim ();

if (Cookname.equals ("Cy")) {

name = Cookname;

value = Cookvalue;

}

}

}

} else

result = "Networkerror";

catch (Connecttimeoutexception e) {//timeout or network connection error

result = "Timeouterror";

E.printstacktrace ();

catch (Clientprotocolexception e) {

result = "Networkerror";

E.printstacktrace ();

catch (Exception e) {

result = "Readerror";

LOG.E (TAG, E.getmessage ());

throw new Exception (e);

finally {

Getmethod.abort ();

}

return result;

}

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.