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: POST Request How to enable Chunk Size
Date Fri, 01 Jun 2012 13:53:25 GMT
On Thu, 2012-05-31 at 09:30 -0700, anr wrote:
> Thanks olegk, Based on your input and help I created below classes and
> started to test it out.
> Now I am able to control the size of the chunk and disable caching with 0
> size.
> 
> How do I use these classes with connection pooling classes. 

Please see this section of the HttpClient tutorial

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/advanced.html#d5e1339

Hope this helps

Oleg

> I am only able to use with HttpRequestExecutor as shown below code, I need
> help to know if it can be coded differently or you can point me to the right
> examples which use HttpClientConnection class.
> 
> InputStream is = (InputStream) request.getInputStream();
> 		BasicHttpParams bhttp_params = new BasicHttpParams();
> 		 HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
> 	        HttpContext context = new BasicHttpContext(null);
> 	   
> 	        HttpHost host = new HttpHost("abcd.efgh.ijk.net",80);
> 			MyHttpClientConnection conn = new MyHttpClientConnection();
> 			conn.setChunkSize(0);
> 			 BasicHttpProcessor httpproc = new BasicHttpProcessor();
> 			 context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
> 				context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
> 			 httpproc.addInterceptor(new RequestContent());
> 		        httpproc.addInterceptor(new RequestTargetHost());
> 		        httpproc.addInterceptor(new RequestConnControl());
> 		        httpproc.addInterceptor(new RequestUserAgent());
> 			if (!conn.isOpen())
> 			{
> 				Socket socket;
> 				try {
> 					socket = new Socket(host.getHostName(), host.getPort());
> 					conn.bind(socket,bhttp_params);
> 				} catch (UnknownHostException e) {
> 					// TODO Auto-generated catch block
> 					e.printStackTrace();
> 				} catch (IOException e) {
> 					// TODO Auto-generated catch block
> 					e.printStackTrace();
> 				}
> 				
> 			}
> 		
> 			BasicHttpEntityEnclosingRequest bhttp_req = new
> BasicHttpEntityEnclosingRequest("POST","/localbusinesssearch/rsa");
> 			 InputStreamEntity is_entity = new InputStreamEntity(is, -1);
> 			  
> 			  is_entity.setContentType("application/octet-stream");
> 			    is_entity.setChunked(true);
> 			    bhttp_req.setEntity(is_entity);
> 			    try {
> 					httpexecutor.preProcess(bhttp_req, httpproc, context);
> 					HttpResponse http_resp = httpexecutor.execute(bhttp_req, conn,
> context);
> 					   
> 					   http_resp.setParams(bhttp_params);
> 					    httpexecutor.postProcess(http_resp, httpproc, context);
> 					    System.out.println("<< Response: " + http_resp.getStatusLine());
> 					    System.out.println("<< Response: " +
> Arrays.toString(http_resp.getAllHeaders()));
> 					    System.out.println(EntityUtils.toString(http_resp.getEntity()));
> 				} catch (HttpException e) {
> 					// TODO Auto-generated catch block
> 					e.printStackTrace();
> 				}
> 
> 
> public class MyHttpClientConnection extends DefaultHttpClientConnection
> {
>     MyEntitySerializer myEntitySerializer;
> 	public MyHttpClientConnection()
> 	{
> 		super();
> 		myEntitySerializer.setChunkSize(0);
> 		
> 	}
> 	
> 	public void setChunkSize(int size)
> 	{
> 		myEntitySerializer.setChunkSize(size);
> 	}
>     
> 	@Override 
>     protected EntitySerializer createEntitySerializer() { 
> 		System.out.println("create my entity serializer");
> 		myEntitySerializer = new MyEntitySerializer(new
> StrictContentLengthStrategy());
>         return myEntitySerializer;
> 	} 
> 
> 	
> 
> }
> 
> public class MyEntitySerializer extends EntitySerializer {
> 	private ContentLengthStrategy lenStrategy;
> 	private int chunkSize;
> 	private int count = 0;
> 	
> 	public MyEntitySerializer(ContentLengthStrategy lenStrategy) {
> 		super(lenStrategy);
> 		 this.lenStrategy = lenStrategy; 
> 	}
> 	
>     @Override 
>     protected OutputStream doSerialize( 
>             SessionOutputBuffer outbuffer, 
>             HttpMessage message) throws HttpException, IOException 
>     { 
>         long len = this.lenStrategy.determineLength(message); 
>         if (len == ContentLengthStrategy.CHUNKED) { 
>             // use largish buffer for output chunks 
>         	return new ChunkedOutputStream(outbuffer,chunkSize); 
>         } else if (len == ContentLengthStrategy.IDENTITY) { 
>             return new IdentityOutputStream(outbuffer); 
>         } else { 
>             return new ContentLengthOutputStream(outbuffer, len); 
>         } 
>     } 
>     
>     public void setChunkSize(int size)
>     {
>     	this.chunkSize = size;
>     }
> 
> 
> }
> 
> 
> 
> 
> olegk wrote:
> > 
> > On Fri, 2012-05-25 at 09:28 -0700, anr wrote:
> >> scenario is end user speaking in the mobile device the audio stream
> >> should be
> >> flushed to the speech engine to process as it comes.
> >> Can you help me out by pointing to the examples how to achieve the steps
> >> you
> >> have mentioned.
> >> 
> >> Thanks for all the help.
> > 
> > static class MyEntitySerializer extends EntitySerializer {
> > 
> >     private ContentLengthStrategy lenStrategy;
> >     
> >     public MyEntitySerializer(ContentLengthStrategy lenStrategy) {
> >         super(lenStrategy);
> >         this.lenStrategy = lenStrategy;
> >     }
> > 
> >     @Override
> >     protected OutputStream doSerialize(
> >             SessionOutputBuffer outbuffer, 
> >             HttpMessage message) throws HttpException, IOException {
> >         long len = this.lenStrategy.determineLength(message);
> >         if (len == ContentLengthStrategy.CHUNKED) {
> >             // use largish buffer for output chunks
> >             return new ChunkedOutputStream(outbuffer, 10240);
> >         } else if (len == ContentLengthStrategy.IDENTITY) {
> >             return new IdentityOutputStream(outbuffer);
> >         } else {
> >             return new ContentLengthOutputStream(outbuffer, len);
> >         }
> >     }
> >     
> > }
> > 
> > static class MyHttpClientConnection extends DefaultHttpClientConnection
> > {
> > 
> >     public MyHttpClientConnection() {
> >         super();
> >     }
> > 
> >     @Override
> >     protected EntitySerializer createEntitySerializer() {
> >         return new MyEntitySerializer(new
> > StrictContentLengthStrategy());
> >     }
> >     
> > }
> > 
> > Hope this helps
> > 
> > Oleg
> > 
> >>  
> >> 
> >> 
> >> olegk wrote:
> >> > 
> >> > On Thu, 2012-05-24 at 15:13 -0700, anr wrote:
> >> >> 
> >> >> DEBUG org.apache.http.wire - >> "800[\r][\n]"
> >> >>  org.apache.http.wire - >> "800[\r][\n]"
> >> >> 
> >> >> CoreConnectionPNames.SOCKET_BUFFER_SIZE
> >> >> CoreConnectionPNames.MIN_CHUNK_LIMIT
> >> >> 
> >> >> How to change the transfer encoding chunk size when request is posted.
> >> I
> >> >> want to set different chunk sizes when we post a request.
> >> >> 
> >> >> Thanks
> >> > 
> >> > Per default HttpClient use 2048 byte cache for chunk coded output. So,
> >> > the chunks should be about 2048 bytes, unless one explicitly uses
> >> #flush
> >> > method to flush out smaller chunks.
> >> > 
> >> > There is currently no (easy) way of overriding the default size of the
> >> > chunk cache without creating a custom entity serializer and custom HTTP
> >> > connection class.
> >> > 
> >> > Oleg    
> >> > 
> >> > 
> >> > ---------------------------------------------------------------------
> >> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> >> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >> > 
> >> > 
> >> > 
> >> 
> > 
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > 
> > 
> > 



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


Mime
View raw message