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 8625549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:48:54+00:00 2026-06-12T07:48:54+00:00

I’m trying to get the cookies from a website after sending user credentials through

  • 0

I’m trying to get the cookies from a website after sending user credentials through a POST Request an it seems that it doesn’t work in android this way. ¿Am I doing something bad?. Please help. I’ve searched here in different posts but there’s no useful answer.

It’s curious that this run in a desktop Java implementation it works perfect but it crashes in Android platform. And it is exactly the same code, specifically when calling HttpURLConnection.getHeaderFields(), it also happens with other member methods. It’s a simple code and I don’t know why the hell isn’t working.

DESKTOP CODE: This goes just in the main()

HttpURLConnection connection = null;
        OutputStream out = null;
        try {                   
            URL url = new URL("http://www.XXXXXXXX.php");           
            String charset = "UTF-8";       

            String postback = "1";
            String user = "XXXXXXXXX";
            String password = "XXXXXXXX";
            String rememberme = "on";
            String query = String.format("postback=%s&user=%s&password=%s&rememberme=%s"
                    , URLEncoder.encode(postback, charset)
                    , URLEncoder.encode(user,charset)
                    , URLEncoder.encode(password, charset)
                    , URLEncoder.encode(rememberme, charset));

            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept-Charset", charset);
            connection.setDoOutput(true);           
            connection.setFixedLengthStreamingMode(query.length());

            out = connection.getOutputStream ();
            out.write(query.getBytes(charset));

            if (connection.getHeaderFields() == null){
                System.out.println("Header null");
            }else{
                for (String cookie: connection.getHeaderFields().get("Set-Cookie")){
                    System.out.println(cookie.split(";", 2)[0]);
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            try { out.close();} catch (IOException e) { e.printStackTrace();}
            connection.disconnect();            
        }

So the output is:

    login_key=20ad8177db4eca3f057c14a64bafc2c9
    FASID=cabf20cc471fcacacdc7dc7e83768880
    track=30c8183e4ebbe8b3a57b583166326c77
    client-data=%7B%22ism%22%3Afalse%2C%22showm%22%3Afalse%2C%22ts%22%3A1349189669%7D

ANDROID CODE: This goes inside doInBackground AsyncTask body

HttpURLConnection connection = null;
            OutputStream out = null;
            try {                   
                URL url = new URL("http://www.XXXXXXXXXXXXXX.php");         
                String charset = "UTF-8";       

                String postback = "1";
                String user = "XXXXXXXXX";
                String password = "XXXXXXXX";
                String rememberme = "on";
                String query = String.format("postback=%s&user=%s&password=%s&rememberme=%s"
                        , URLEncoder.encode(postback, charset)
                        , URLEncoder.encode(user,charset)
                        , URLEncoder.encode(password, charset)
                        , URLEncoder.encode(rememberme, charset));

                connection = (HttpURLConnection)url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Accept-Charset", charset);
                connection.setDoOutput(true);           
                connection.setFixedLengthStreamingMode(query.length());

                out = connection.getOutputStream ();
                out.write(query.getBytes(charset));

                if (connection.getHeaderFields() == null){
                    Log.v(TAG, "Header null");
                }else{
                    for (String cookie: connection.getHeaderFields().get("Set-Cookie")){
                        Log.v(TAG, cookie.split(";", 2)[0]);
                    }
                }
            } catch (IOException e){
                e.printStackTrace();
            } finally {
                try { out.close();} catch (IOException e) { e.printStackTrace();}
                connection.disconnect();            
            }

And here there is no output, it seems that connection.getHeaderFields() doesn’t return result. It takes al least 30 seconds to show the Log:

10-02 16:56:25.918: V/class com.giorgi.myproject.activities.HomeActivity(2596): Header null

TESTED ON A GALAXY NEXUS

  • 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-06-12T07:48:56+00:00Added an answer on June 12, 2026 at 7:48 am

    I’ve figured out what was the problem. It seems that using Java Desktop, the flag FollowRedirects is false by default (I suppose) and in Android it is true. getInstanceFollowRedirects is TRUE in both cases, so I don’t really know why it works in a different way, but nevermind, the solution is perfect.

    So it wasn’t capturing the response of the POST request, it was following some redirection and trying to get the response from another GET auto request.

    The solution was to do: connection.setInstanceFollowRedirects(false);


    The way I could know this was looking into the network traffic using a network monitor:

    From the Desktop application it was monitored this traffic:

    23  10:08:50 03/10/2012 1.3944670   javaw.exe   192.168.1.36    www.XXXXXXXXX.com   HTTP    HTTP:Request, POST /es/login.php    {HTTP:8, TCP:7, IPv4:6}
    24  10:08:50 03/10/2012 1.3954741   javaw.exe   192.168.1.36    www.XXXXXXXXX.com   HTTP    HTTP:HTTP Payload, URL: /es/login.php   {HTTP:8, TCP:7, IPv4:6}
    32  10:08:50 03/10/2012 1.9811257   javaw.exe   www.XXXXXXXXX.com   192.168.1.36    HTTP    HTTP:Response, HTTP/1.1, Status: Moved temporarily, URL: /es/login.php  {HTTP:8, TCP:7, IPv4:6}
    

    To monitor the traffic in Android application I had to run it in the emulator instead the phone.The result was:

    60  9:59:34 03/10/2012  4.0285909   emulator-arm.exe    192.168.1.36    XX.XX.XXX.XXX   HTTP    HTTP:Request, POST /es/login.php    {HTTP:13, TCP:12, IPv4:11}
    65  9:59:34 03/10/2012  4.1524735   emulator-arm.exe    192.168.1.36    XX.XX.XXX.XXX   HTTP    HTTP:HTTP Payload, URL: /es/login.php   {HTTP:13, TCP:12, IPv4:11}
    75  9:59:35 03/10/2012  4.6276286   emulator-arm.exe    XX.XX.XXX.XXX   192.168.1.36    HTTP    HTTP:Response, HTTP/1.1, Status: Moved temporarily, URL: /es/login.php  {HTTP:13, TCP:12, IPv4:11}
    77  9:59:35 03/10/2012  4.7095994   emulator-arm.exe    192.168.1.36    XX.XX.XXX.XXX   HTTP    HTTP:Request, GET /es/login.php, Query:FASID=a5e39f35325499e060f43d35bc956a45   {HTTP:13, TCP:12, IPv4:11}
    311 9:59:55 03/10/2012  24.8355823  emulator-arm.exe    XX.XX.XXX.XXX   192.168.1.36    HTTP    HTTP:Response, HTTP/1.1, Status: Moved temporarily, URL: /es/login.php  {HTTP:13, TCP:12, IPv4:11}
    313 9:59:55 03/10/2012  24.9384843  emulator-arm.exe    192.168.1.36    XX.XX.XXX.XXX   HTTP    HTTP:Request, GET /es/main.html, Query:FASID=a5e39f35325499e060f43d35bc956a45   {HTTP:13, TCP:12, IPv4:11}
    317 9:59:55 03/10/2012  25.0535818  emulator-arm.exe    XX.XX.XXX.XXX   192.168.1.36    HTTP    HTTP:HTTP Payload, URL: /es/main.html   {HTTP:13, TCP:12, IPv4:11}
    

    So, after applying the **connection.setInstanceFollowRedirects(false);** the result was the expected:

    61  10:30:43 03/10/2012 4.9211205   emulator-arm.exe    192.168.1.36    XX.XX.XXX.XXX   HTTP    HTTP:Request, POST /es/login.php    {HTTP:14, TCP:13, IPv4:12}
    64  10:30:43 03/10/2012 5.0362501   emulator-arm.exe    192.168.1.36    XX.XX.XXX.XXX   HTTP    HTTP:HTTP Payload, URL: /es/login.php   {HTTP:14, TCP:13, IPv4:12}
    70  10:30:43 03/10/2012 5.5103384   emulator-arm.exe    XX.XX.XXX.XXX   192.168.1.36    HTTP    HTTP:Response, HTTP/1.1, Status: Moved temporarily, URL: /es/login.php  {HTTP:14, TCP:13, IPv4:12}
    

    Thank you for all your answers and interest.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.