hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: very simple problem... how to post data to an HttpConnection...
Date Tue, 26 Oct 2004 21:30:12 GMT
Mike,
In fact, we strongly discourage the direct use of HttpConnection class.
The preferred way is to let the connection manager take care of the HTTP
connection business. Unless you have a real reason to use HttpConnection
class directly, I'd recommend that you use HttpClient instead

Something like that:
=====================================================================
HttpClient client = new HttpClient();
PostMethod httppost = new PostMethod("http://www.whatever.com");

File file = new File(args[0]);
httppost.setRequestBody(new FileInputStream(file));
httppost.setRequestContentLength((int)file.length());

try {
  client.executeMethod(httppost);
  if (httppost.getStatusCode() != HttpStatus.SC_OK) {
    System.out.println("Unexpected failure: " + httppost.getStatusLine());
  }
  System.out.println(httppost.getResponseBodyAsString());
} finally {
  httppost.releaseConnection();
}
=====================================================================

BTW, you are the first ever user to post to the httplclient-user list

;-)

Cheers,

Oleg


On Tue, 2004-10-26 at 22:52, Mike Y wrote:
> As you might guess from the subject, I'm new here
> 
> I'm attempting to use the 2.0.2 version of this
> project to create a connection, post data to it, and
> read the results, or at least get a Stream of output
> that I can pass someplace else.
> 
> I've been reading the Javadocs and tutorials for the
> last hour at least, the tutorials seem to reference an
> HttpMethod class which can't actually be found in the
> Javadocs or distribution. 
> 
> So, I'm not sure what I'm supposed to do. I have:
> 
> 1. String postRequest = " ... ";
> 2. HttpConnection c = new HttpConnection
> ("http://somesite/somedynamicpage.asp", 80);
> 3. c.setConnectionTimeout(someTimeoutValue);
> 
> 4. --- Need to post my request to my connection --
> 
> return c.getResponseInputStream();
> 
> I would greatly appreciate any help here.  I hope this
> is not a stupid question.  I saw and read the docs on
> the PostMethod, but it's not clear to me how this
> class interacts with HttpConnection.  This is likely
> the result of my own mental deficiencies.  
> 
> For what it is worth, the tutorial I was looking at
> was here:
> 
> http://cvs.apache.org/viewcvs.cgi/jakarta-commons/httpclient/src/examples/Attic/CustomHttpConnection.java?only_with_tag=HTTPCLIENT_2_0_BRANCH&view=markup).
> 
> --Mike
> 
> 
> 
> 		
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete - You start. We finish.
> http://promotions.yahoo.com/new_mail
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 


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


Mime
View raw message