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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:02:49+00:00 2026-05-29T23:02:49+00:00

I want to login my gmail and get the contact list automatically with httpClient,

  • 0

I want to login my gmail and get the contact list automatically with httpClient,
I’ve tryed the method described in the page below:
Android: How to login into webpage programmatically, using HttpsURLConnection
but once it ran to:
String cookie = response.getFirstHeader("Set-Cookie").getValue();
a java.lang.NullPointerException was catched.
I thought it was because the page was moved temporarily, then I coded my code like this:

    private static String uriLogin = "https://mail.google.com";
private static String uriContacts = "https://mail.google.com/mail/shva=1#contacts";

// the account was registered just for test:
private static String myAcc = "httpclient.test";
private static String myPwd = "testpassword";


public static void main(String[] args) throws ClientProtocolException,
        IOException, InterruptedException {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(uriLogin);

    List<NameValuePair> params = new ArrayList<NameValuePair>(3);
    params.add(new BasicNameValuePair("Email", myAcc));
    params.add(new BasicNameValuePair("Passwd", myPwd));
    params.add(new BasicNameValuePair("signIn", "Sign in"));
    post.setEntity(new UrlEncodedFormEntity(params));
    redp("url is:: ", post.getEntity());
    HttpResponse rsp = client.execute(post);

    if (rsp.getStatusLine().getStatusCode() >= 400) {// returns 302 if
                                                        // success
        System.err.println("failed to get the web page!!!");
        System.err.println("status: " + rsp.getStatusLine());
        System.exit(-1);
    }
    redp("status is:: ", rsp.getStatusLine());
    redp("heads of rsp :: ", "");
    pHeads(rsp);
    redp("content is:: ", EntityUtils.toString(rsp.getEntity()));

    String redirect = rsp.getLastHeader("Location").getValue();
    HttpGet get = new HttpGet(redirect);
    rsp = client.execute(get);

    String cookie = rsp.getFirstHeader("Set-Cookie").getValue();
    redp("cookie is:: ", cookie);

    HttpGet getContacts = new HttpGet(uriContacts);
    getContacts.setHeader("Cookie", cookie);
    redp("heads of get [contacts]:: ", "");
    pHeads(getContacts);
    client.getConnectionManager().shutdown();
    client = new DefaultHttpClient(); // without these 2 lines,
                                        // "java.lang.IllegalStateException"
                                        // will be catched
    rsp = client.execute(getContacts);
    redp("heads of rsp (new) ::", "");
    pHeads(rsp);

    InputStream istream = rsp.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            istream));

    String line;
    p("联系人列表: ");
    while ((line = reader.readLine()) != null) {
        p(line);
    }
    reader.close();
    istream.close();

}// main

public static void pHeads(HttpMessage msg) {
    Header[] headers = msg.getAllHeaders();
    for (int i = 0; i < headers.length; i++)
        p(headers[i].getName() + ": " + headers[i].getValue());

}

public static void p(Object o) {
    System.out.println(o);
}

public static void redp(String head, Object o) throws InterruptedException {
    System.err.println(head);
    if (o.equals("") || o.equals(null))
        return;
    Thread.sleep(100);
    System.out.println(o);
}

}
`
but it still doesn’t work… Any help would be great~~
[BTW, I saw a some people says on the Internet that httpClient was not very acceptable for this kind of job, could you tell me in what kind of project HttpClient is most used?]

  • 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-29T23:02:50+00:00Added an answer on May 29, 2026 at 11:02 pm

    From what I’ve seen, You receive the nullPointerEception when you get to response.getLastHeader(“Location”).getValue();
    it looks like there is no “Location” tag in the GMail html header. When Java fails to find this, it just throws “I can’t find it”

    What you want is the redirect after the POST occurs.
    At a quick glance, its something like:
    https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2
    but you should double check this.
    Hope this helps.

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

Sidebar

Related Questions

I want to integrate facebook login into my web application developed using Spring MVC.
I want to get the contents of some emails in my gmail account. I
I want my website user should be login through fbconnect or gmail account. fbconnect
I am using asp.net 3.5 with C#. I want to integrate gmail into my
I want to know how stackoverflow.com enables login via my Gmail Account. Is there
i want to read the mails from gmail account.. i tried to login.. and
I want to login to Amazon seller central programmatically with C#. I don't want
I want to implement login using facebook in my windows phone 7.1 application When
I want to support login with an MSN account but MSN doesn't support OpenID.
I want to create login and password fields same as in facebook app for

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.