hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From umesh...@yahoo.com
Subject How to ensure socket reuse on client side?
Date Wed, 08 Nov 2006 20:46:41 GMT
I am new to HttpCore and using 4.0 Alpha 2 version. I am trying to connect a server and request
more than one pages from that server using the same connection. My goal is to reuse socket
so that no new socket is opened on the Http client for the subsequent requests to the same
server and port.

I get error on the second request. The output / exception stack is as follows:

<< Request: GET / HTTP/1.1
<< Send at: Wed Nov 08 11:57:47 PST 2006
>> Response: HTTP/1.1 200 OK
<< Received: Wed Nov 08 11:57:47 PST 2006
>> Headers Name: Date Value: Wed, 08 Nov 2006 19:57:47 GMT
>> Headers Name: Server Value: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.7g
>> Headers Name: Last-Modified Value: Wed, 01 Nov 2006 10:45:24 GMT
>> Headers Name: ETag Value: "d4813c-45be-4212d33413900"
>> Headers Name: Accept-Ranges Value: bytes
>> Headers Name: Content-Length Value: 17854
>> Headers Name: Keep-Alive Value: timeout=5, max=100
>> Headers Name: Connection Value: Keep-Alive
>> Headers Name: Content-Type Value: text/html
==============
Connection kept alive...
<< Request: GET /httpcomponents/index.html HTTP/1.1
<< Send at: Wed Nov 08 11:57:48 PST 2006
org.apache.http.ProtocolException: The server jakarta.apache.org failed to respond with a
valid HTTP response
	at org.apache.http.impl.DefaultHttpClientConnection.readResponseStatusLine(DefaultHttpClientConnection.java:278)
	at org.apache.http.impl.DefaultHttpClientConnection.receiveResponseHeader(DefaultHttpClientConnection.java:222)
	at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:421)
	at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:204)
	at org.apache.http.examples.ElementalHttpGet.main(ElementalHttpGet.java:118)

If anyone knows the links where I can get more information, that will help. I have modelled
my code on the lines of ElementalHttpGet example. The code is as follows:

package org.apache.http.examples;

 

import org.apache.http.ConnectionReuseStrategy;

import org.apache.http.Header;

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.HttpVersion;

import org.apache.http.Scheme;

import org.apache.http.impl.DefaultConnectionReuseStrategy;

import org.apache.http.impl.DefaultHttpClientConnection;

import org.apache.http.impl.DefaultHttpParams;

import org.apache.http.impl.io.PlainSocketFactory;

import org.apache.http.io.SocketFactory;

import org.apache.http.message.HttpGet;

import org.apache.http.params.HttpParams;

import org.apache.http.params.HttpProtocolParams;

import org.apache.http.protocol.HttpContext;

import org.apache.http.protocol.HttpExecutionContext;

import org.apache.http.protocol.HttpRequestExecutor;

import org.apache.http.protocol.RequestConnControl;

import org.apache.http.protocol.RequestContent;

import org.apache.http.protocol.RequestExpectContinue;

import org.apache.http.protocol.RequestTargetHost;

import org.apache.http.protocol.RequestUserAgent;

 

/**

 * <p>

 * </p>

 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>

 *

 * @version $Revision: 376458 $

 */

public class ElementalHttpGet {

 

   public static void main(String[] args) throws Exception {

 

      SocketFactory socketfactory = PlainSocketFactory.getSocketFactory();

      Scheme.registerScheme("http", new Scheme("http", socketfactory, 80));

 

      HttpParams params = new DefaultHttpParams(null);

      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

      HttpProtocolParams.setContentCharset(params, "UTF-8");

      HttpProtocolParams.setUserAgent(params, "Jakarta-HttpComponents/1.1");

      HttpProtocolParams.setUseExpectContinue(params, true);

           

      HttpHost host = new HttpHost("jakarta.apache.org", 80);

      

      HttpContext context = new HttpExecutionContext(null);

      context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, host);

      

      HttpRequestExecutor httpexecutor = new HttpRequestExecutor(context);

      httpexecutor.setParams(params);

      // Required protocol interceptors

      httpexecutor.addInterceptor(new RequestContent());

      httpexecutor.addInterceptor(new RequestTargetHost());

      // Recommended protocol interceptors

      httpexecutor.addInterceptor(new RequestConnControl());

      httpexecutor.addInterceptor(new RequestUserAgent());

      httpexecutor.addInterceptor(new RequestExpectContinue());

 

      DefaultHttpClientConnection conn = new DefaultHttpClientConnection(host);

      ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

      

      try {

       

         String[] targets = {

                              "/",

                              "/httpcomponents/index.html",

                              "/httpcomponents/download.html"         };

         

                          

            for (int i = 0; i < targets.length; i++) {

               

               HttpGet request = new HttpGet(targets[i]);

 

               System.out.println("<< Request: " + request.getRequestLine());

               

               HttpResponse response = httpexecutor.execute(request, conn);

 

               System.out.println(">> Response: " + response.getStatusLine());

               

               Header[] hds = response.getAllHeaders();

               for (Header h: hds){

                  System.out.println(">> Headers Name: " + h.getName() + 

                        " Value: " + h.getValue() );

               }

               

               System.out.println("==============");

               

               if (!connStrategy.keepAlive(response)) {

                  System.out.println("Connection closed.");

                  conn.close();

               } else {

                  conn.flush();

                  System.out.println("Connection kept alive...");

               }

 

            }

                        

      } catch(Exception exp){

         exp.printStackTrace();

      }

      finally {

         conn.close();

      }

   }

 

}


Any help is appreciated.

Thanks.

--
This message was sent on behalf of umesh409@yahoo.com at openSubscriber.com
http://www.opensubscriber.com/messages/httpclient-dev@jakarta.apache.org/topic.html

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


Mime
View raw message