Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 562655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:34:39+00:00 2026-05-13T12:34:39+00:00

I’m building an android app which should perform a GET on my site to

  • 0

I’m building an android app which should perform a GET on my site to get two cookies and then perform a post to the same site with these cookies.

As mentioned I start of with the GET and I’m using org.apache.http.client.HttpClient to perform this operation.

String requiredCookies = "";
HttpContext localContext = null;

System.out.println("------------------GET----------------------");
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("www.mysitegeturl.com");

//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();

// Creating a local HTTP context
localContext = new BasicHttpContext();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);

HttpResponse response;
try {
    response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
   System.out.println("Response content length: " + entity.getContentLength());
}

//Do this so that Java.net impl should work
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
     requiredCookies += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
}

if (entity != null) {
    entity.consumeContent();
}

} catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
 e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
System.out.println("------------------GET-END---------------------");

So far so good. Don’t mind the requiredCookies line yet, it will be used in the Java.net impl since I can’t get the HttpClient one to work =(.
Let’s take a look at the non working HttpClient Post part.

System.out.println("------------------HttpClient - POST----------------------");
HttpPost post = new HttpPost("www.mysiteposturl.com");

//Params       
HttpParams params = new BasicHttpParams();

params.setParameter("foo", "post");
params.setParameter("bar", "90");
params.setParameter("action", "search");

post.setParams(params);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");

try {
    HttpResponse response2 = httpClient.execute(post, localContext);
     System.out.println(response2.getStatusLine());
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
System.out.println("------------------POST END---------------------");

What happens now is that I perform a POST with the localContext where the cookies are stored. This doesn’t work. I get a HTTP/1.1 401 No session. Since I had no luck with this I tried another approach(java.net.HttpURLConnection). Remember I still use the same GET part

URL url = new URL("www.mysiteposturl");
HttpURLConnection connection = null;

String dataString = "bar=90&foo=post&action=search";

try {
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Cookie", requiredCookies);
//Set to POST
connection.setDoOutput(true);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(dataString);
writer.flush();
writer.close();
connection.connect();

if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
    System.out.println(connection.getContent().toString());
} else {
   System.out.println("Error");
}
} catch (IOException e) {
   // TODO Auto-generated catch block
     e.printStackTrace();
} catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

System.out.println("------------------POST END---------------------");

And VIOLA a 200 is displayed and everything works like a charm. What do you guys think? Could someone please provide me with an answer because I can’t figure it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:34:39+00:00Added an answer on May 13, 2026 at 12:34 pm

    The problem appears to be that you have two different host names in the setup. This will cause HTTP Client to not send cookies for a different host. You could try changing the domain of the cookies in the cookie store, or using the same host for GET and POST. Additionally you could manually add the cookies to the headers in HTTP Client as you did in the HttpURLConnection example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.