Results 1 to 1 of 1
  1. #1
    tambeo91 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default How to use HttpClient 4.1 to login Twitter

    I want to do something with twitter. but i can't login it by httpclient 4.1.
    i just learn httplient so please help me.
    Java Code:
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://twitter.com/sessions?phx=1");
    //--- add name and pass to post method
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
    nameValuePairs.add(new BasicNameValuePair("session[username_or_email]", "*******"));
    nameValuePairs.add(new BasicNameValuePair("session[password]", "50902349"));   
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));        
    //-- excute client
    HttpResponse response = client.execute(post);
    HeaderElementIterator H = new BasicHeaderElementIterator(response.headerIterator("Set-Cookie"));
    String string = "";
    // --- get all Set-Cookie
    while(H.hasNext())
            {
            	HeaderElement element = H.nextElement();
            	string+=element.getName()+"="+element.getValue()+";";
            }        
    System.out.println("----------------");
    HttpPost Post_result = new HttpPost("http://twitter.com");
    HttpClient Client_result = new DefaultHttpClient();
            // --- add header Cookie to post method
    Post.setHeader("Cookie", string);
    HttpResponse Response_result = Client_result.execute(Post_result);
    HttpEntity entity = Response_result.getEntity();
            // print the page result but it can't login
    System.out.print(EntityUtils.toString(entity));
    Last edited by tambeo91; 08-10-2011 at 03:15 PM.

Similar Threads

  1. How to use Httpclient to login into a web page
    By tambeotb4 in forum Networking
    Replies: 1
    Last Post: 05-28-2011, 09:34 PM
  2. Twitter Open-ended Biographical Search Engine
    By mobiletimes in forum Java Software
    Replies: 0
    Last Post: 11-23-2010, 11:26 AM
  3. Replies: 3
    Last Post: 09-14-2010, 02:37 PM
  4. login with cookies using Apache httpClient
    By MacGyver in forum Advanced Java
    Replies: 0
    Last Post: 05-15-2010, 03:21 PM
  5. Replies: 0
    Last Post: 06-25-2008, 12:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •