Quick Start

To quickly begin with the beta you need six jar files that can be downloaded from http://www.apache.org/dist/httpcomponents/httpclient/binary/httpcomponents-client-4.0-beta2-bin-with-dependencies.zip

A good place to learn how to use the API quickly is to see the code in action. Can see good samples that have several everyday tasks automated. They are included in the jars at download page http://hc.apache.org/downloads.cgi . Good starter material is also at http://wiki.apache.org/HttpComponents/GuidedTourOfHttpCore Remember Http Components are not a complete browser. Importantly it lacks parsers, layout engine, UI, cache, renderer and a JavaScript engine. To learn the scope of these components see http://hc.apache.org/httpcomponents-client/primer.html

Note: this guide is not complete and contributions are welcome. To contribute write to dev at hc dot apache.org (first subscribe by sending a short email to dev-subscribe  a t hc dot apache.org

Example

package org.apache.http.examples.client;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 * based on 2008-2009 version 4 API http://hc.apache.org/
 */
public class ClientFormLogin {

    public static void main(String[] args) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
                "org=self_registered_users&" +
                "goto=/portal/dt&" +
                "gotoOnFail=/portal/dt?error=true");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("IDToken1", "username"));
        nvps.add(new BasicNameValuePair("IDToken2", "password"));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
    }
}

Where to go from here

http://wiki.apache.org/HttpComponents/FrequentlyAskedApplicationDesignQuestions

QuickStart (last edited 2009-09-20 21:44:14 by localhost)