commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Sutton <Adrian.Sut...@Ephox.com>
Subject RE: httpClient help - for Oleg
Date Wed, 01 Oct 2003 21:44:05 GMT
Hi all,
Can I suggest that if you're new to HttpClient the tutorial is a must read.
It's available at http://jakarta.apache.org/commons/httpclient/tutorial.html
and outlines the best practices for using HttpClient.  Should save you a lot
of headaches in the future.

Regards,

Adrian Sutton, Software Engineer
Ephox Corporation
www.ephox.com 

-----Original Message-----
From: Oleg Kalnichevski [mailto:olegk@bluewin.ch]
Sent: Thursday, 2 October 2003 6:07 AM
To: Jakarta Commons Users List
Subject: Re: httpClient help - for Oleg



David,

Have a look at this one

http://cvs.apache.org/viewcvs.cgi/jakarta-commons/httpclient/src/examples/Po
stXML.java?rev=HEAD

I suppose it is quite close to what you are trying to accomplish. 

Another note: we strongly encourage that HttpClient class be used to
execute HTTP methods, leaving up to HttpClient to do all the low level
connection management. Explicit HTTP connection management is usually
discouraged:

HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod("http://www.whatever.com/target-url");
try {
  post.setRequestBody(new FileInputStream(input));
  httpclient.executeMethod(post);
} finally {
  post.releaseConnection();
}

Hope this helps

Cheers

Oleg

On Wed, 2003-10-01 at 22:04, Jenkins, David wrote:
> Hi Oleg,
> 
> I'll include some code fragments that I currently use and code fragments
> that I've tried using http client. Maybe you can see where my problems
> lie.
> ****
>       HttpURLConnection  connection =
> (HttpURLConnection)url.openConnection();
>       connection.setDoInput(true);
>       connection.setDoOutput(true);
>       connection.setUseCaches(false);
>       connection.setRequestProperty("Content-Type",
>                              "application/x-www-form-urlencoded");
> 
>       ObjectOutputStream oStream =
>           new ObjectOutputStream(connection.getOutputStream());
>       oStream.writeObject(proxy.getClass().getName());
>       oStream.writeObject(method.getName());
>       if (args != null) {
>         for (int i = 0; i < args.length; i++) {
>           oStream.writeObject(args[i]);
>         }
>       }
>       oStream.flush();
>       oStream.close();
> 
>       /* Connect and recover any return args*/
>       connection.connect();
> ****
> I then go on to collect any returned values/exceptions from the
> response.
> 
> 
> This code written for http client simulates a single call 
> 
> ****
>   HttpClient httpClient = new HttpClient();
>   HostConfiguration hostConfiguration  = new HostConfiguration();
>   HttpConnectionManager httpConnectionManager;
> 
>   public Main() {
>     hostConfiguration.setHost("localhost",8080,"http");
>     httpClient.setHostConfiguration(hostConfiguration);
>     httpConnectionManager = httpClient.getHttpConnectionManager();
>     doit();
>   }
> 
> 
>   public void doit() {
>     PostMethod post = null;
>     try {      
>       post = new PostMethod("/fgdam_tst/rPCAction.do");
>       post.setDoAuthentication(true);
> 
>       Header header = new
> Header("content-type",PostMethod.FORM_URL_ENCODED_CONTENT_TYPE);
>       post.setRequestHeader(header);
> 
>       HttpState state = new HttpState();
> 
>       HttpConnection connection =
>           httpConnectionManager.getConnection(hostConfiguration);
> 
>       ObjectOutputStream oos =
>           new ObjectOutputStream(connection.getRequestOutputStream());
> 
>       oos.writeObject("");
>       oos.writeObject("findAllClassifications");
>       oos.flush();
>       oos.close();
> 
>       post.execute(state,connection); 
> 
>       if (post.getStatusCode()==HttpStatus.SC_OK) {
>         ObjectInputStream ois =
>             new ObjectInputStream(connection.getResponseInputStream());
>         Object object = ois.readObject();
>         System.out.println(object);
>       }
>       else{
>         System.out.println(post.getStatusText());
>       }
> 
>       post.releaseConnection();
> 
>     }
>     catch (Exception ex) {
>       ex.printStackTrace();
>     }
>     finally {
>       post.releaseConnection();
>     }
>   }
> 
> ****
> This code results in IOExceptions at the server when trying to read the
> request input stream.
> 
> So far I've not found any code in the samples that does exactly what I
> want. Or more likely I've just not noticed the code that does exactly
> what I want.
> 
> cheers
> 
> Dave
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message