On Wed, 2008-09-17 at 00:20 -0400, Zhaohua Meng wrote: > I have code snippet like the following. After establishing a > connection, I need to steam content from database as post body. How do > I get a OutputStream from either HttpClient or PostMethod? With > java.net.URL I can use openConnection().getOutputStream(). However I > want to leverage the nice features of HttpCleint. In the following > code, I want to call the writeContent() method which should get huge > data from database. >
Zhaohua, You need to implement a custom RequestEntity http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/RequestEntity.html You can use the FileRequestEntity class shipped with HttpClient 3.1 as an example http://hc.apache.org/httpclient-3.x/xref/org/apache/commons/httpclient/methods/FileRequestEntity.html Hope this helps Oleg > Any help is appreciated. > > public static void main(String[] args) throws Exception { > HttpClient client = new HttpClient(); > PostMethod post = new > PostMethod("http://myhost/service/consumer"); > try { > //how to get a OutputStream to write my content? > > int status = client.executeMethod(post); > > System.out.println(post.getResponseBodyAsString()); > } finally { > post.releaseConnection(); > } > } > public static void writeContent(OutputStream out) throws IOException { > //data will be generated from database. > out.write("<data><value>this</value></data>".getBytes()); > } > } > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
