Le Jeudi 18 Avril 2002 10:17, vous avez �crit :
> Bonjour,
>
> Dans le cadre d'un projet, je cherche a acc�der � un serveur HTTP depuis
> mon appli Java. Je cherche � poster des fichiers vers ce serveur en
> utilsant des requetes HTTP(POST+authentification login et mot de passe en
> mode basic).
>
> Avez vous des exemples sous la main, car je trouve que l'API Sun est bien
> maigre ?
> Avez vous des id�es afin de mettre en place ce module?
>
> Merci d'avance

Regarde la lib HTTPClient � l'adresse 
http://www.innovation.ch/java/HTTPClient. Elle est simple � utilser et elle 
fonctionne bien.

Ci dessous un exemple de code :

// standard imports
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ParseException;
import HTTPClient.ModuleException;
import HTTPClient.NVPair;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.Hashtable;

public class Connection {

    private final static String HOST     = "www.toto.com";
    private final static String URI_BASE = "/service/?";
    private final static int    PORT     = 80;

    public Connection () {
    }

    public String submit( Hashtable hash ) {
        if( hash == null ) {
            return "No content for this query";
        }

        String text = "Site not responding";
        HTTPConnection connection = new HTTPConnection( HOST, PORT );
        HTTPResponse   response;

        try {
            response = connection.Post( URI_BASE, createForm( hash ) );

            if (response.getStatusCode() >= 300)        {
                System.err.println( "Received Error: " + response.getReasonLine() );
                text = response.getText();
            } else {
                text = format( response.getText() );
            }
        } catch ( IOException ex ) {
            return "[error] Connection::submit : \n" + ex.toString();
        } catch ( ParseException ex ) {
            return "Error parsing Content-Type: " + ex.toString();
        } catch ( ModuleException ex ) {
            return "Error handling request: " + ex.getMessage();
        }

        return text;
    }

    private NVPair[] createForm( Hashtable hash ) {
        NVPair      form[] = new NVPair[ hash.size() ];
        Enumeration enum   = hash.keys();

        for( int i = 0; enum.hasMoreElements(); i++ ) {
            String   key      = (String)enum.nextElement();
            List     values   = (List)hash.get( key );
            Iterator iterator = values.iterator();

            while( iterator.hasNext() ) {
                form[i] = new NVPair( key, (String)iterator.next() );
            }
        }

        return form;
    }

    protected String format( String str ) {
        return str;
    }

}


Jerome Ramette.

Répondre à