Hi Roland,

I am implementing the connection timeout as shown below:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import
org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;


public class TestConnectiontimeOut
{

        private static final String HTTP_METHOD_GET = "get";
        private static final String HTTP_METHOD_POST = "post";
        private static final String REQUEST_HEADER_USER_AGENT_NAME =
"User-Agent";
        private static final String REQUEST_HEADER_USER_AGENT_VALUE =
"MSIE 6.0";
        private static final String REQUEST_HEADER_CONTENT_TYPE_NAME =
"Content-type";
        private static final String REQUEST_HEADER_CONTENT_TYPE_VALUE =
"text/xml; charset=ISO-8859-1";
        private static final String REQUEST_HEADER_SOAP_ACTION_NAME =
"SOAPAction";

        //private String host = "172.22.14.136";
        private String host = "172.24.19.79";
        private int port = 7001;
        private String realm = "myrealm";
        private String authenticationScheme = "BASIC";
        private String userName = "tester";
        private String password = "tester123";

        HttpMethodBase methodClass = null;
        String response = null;
        RequestEntity entity = null;

        private String methodName = "POST";
        private boolean isSoap = true;
        private String xmlContents = "<SOAP:Envelope><SOAP:Header
xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\";><header><reques
tID>client1:0</requestID><securitie><password>cramer12</password><user>t
estUser</user></securitie></header></SOAP:Header><SOAP:Body><SLPServiceT
ype>video.uniroma1.it</SLPServiceType><faultfilter><and>equal</and></fau
ltfilter><ingressLabelSwitchRouterId>1</ingressLabelSwitchRouterId><prov
isionedPathId>2</provisionedPathId><tunnelInstance>1</tunnelInstance></S
OAP:Body></SOAP:Envelope>";
        private String soapAction = "";
        private String command = "SimpleWebSimulator/SOAPAgent";

        public TestConnectiontimeOut() 
        {
                MultiThreadedHttpConnectionManager connManager = new
MultiThreadedHttpConnectionManager();
                HostConfiguration hostConfig = new HostConfiguration();
                hostConfig.setHost(host,port);
                HttpClient httpclient = new HttpClient(connManager);
                httpclient.setHostConfiguration(hostConfig);
        
httpclient.getParams().setAuthenticationPreemptive(true);

                httpclient.getState().setCredentials(
                new AuthScope(host, port, realm,authenticationScheme),
                new UsernamePasswordCredentials(userName, password));

                //logger.debug("sendCommand start");
                String url = "http://"+host+":"+port+"/"+command;
                
                
                if(methodName.equalsIgnoreCase(HTTP_METHOD_GET))
        {
                        methodClass = new GetMethod(url);
        
methodClass.setRequestHeader(REQUEST_HEADER_USER_AGENT_NAME,REQUEST_HEAD
ER_USER_AGENT_VALUE);
        
methodClass.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILI
TY);
        }
        else if(methodName.equalsIgnoreCase(HTTP_METHOD_POST))
        {
                 methodClass = new PostMethod(url);
                 if(isSoap)
                 {
                         //File test = this.createFile(xmlFileName);
                         //entity = new InputStreamRequestEntity(new
FileInputStream(test), InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
                         byte [] byteInput = xmlContents.getBytes();
                         ByteArrayInputStream bi = new
ByteArrayInputStream(byteInput);
                         entity = new InputStreamRequestEntity(bi,
InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
 
((PostMethod)methodClass).setRequestEntity(entity);
 
methodClass.setRequestHeader(REQUEST_HEADER_CONTENT_TYPE_NAME,
REQUEST_HEADER_CONTENT_TYPE_VALUE);
 
methodClass.setRequestHeader(REQUEST_HEADER_SOAP_ACTION_NAME,
soapAction);
                         
                         
                 }
        }
                methodClass.setDoAuthentication( true );
                
                synchronized(this) 
                {
            try 
            {
                this.wait(250);
            } 
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
        }
                
        
        
        
                try
        {
                        //int status = httpclient.executeMethod(
methodClass );
                                HttpConnection httpConnection =
httpclient.getHttpConnectionManager().getConnectionWithTimeout(httpclien
t.getHostConfiguration(),10000);
                                httpConnection.open();
                                HttpState httpState =
httpclient.getState();
                                UsernamePasswordCredentials cred =
(UsernamePasswordCredentials)httpState.getCredentials(new
AuthScope(host, port, realm,authenticationScheme));
                                System.out.println("username is
"+cred.getUserName());
                                System.out.println("password is
"+cred.getPassword());
                                int status =
methodClass.execute(httpclient.getState(),httpConnection);
                        response = status
+"\n"+methodClass.getResponseBodyAsString();
                        //logger.debug("before printing response");
                //logger.debug("response is "+response);
                        //return response;
                                System.out.println(response);
                
        }
                
                catch(Exception e)
                {
                        if(e instanceof ConnectTimeoutException)
                        {
                                //logger.debug("ConnectTimeoutException
occured in send");
        
System.out.println("ConnectTimeoutException occured in send");
                        }
                        else
                        {
                                e.printStackTrace();
                        }
                        //return response;
                }
                
        finally
        {
        //logger.debug("sendCommand end");
        //logger.debug("isOpen =
"+client.getHttpConnectionManager().getConnection(client.getHostConfigur
ation()).isOpen());
        //logger.debug("closeIfStale =
"+client.getHttpConnectionManager().getConnection(client.getHostConfigur
ation()).closeIfStale());
        methodClass.releaseConnection();
        }
        

        }
        public static void main(String[] args) 
        {
                //System.out.println("Hello World!");
                new TestConnectiontimeOut();
        }
}

When I am using HttpConnection I am getting following error:

Error 401--Unauthorized

When I am using HttpClient instead of HttpConnection it is working fine.

Could you please help me in resolving the issue?

Regards,
Lalit


-----Original Message-----
From: Roland Weber [mailto:[EMAIL PROTECTED]
Sent: Friday, December 08, 2006 1:19 PM
To: HttpClient User Discussion
Subject: RE: How to test Connection timeout


Hello Lalit,

> When I am using clientParams.setSoTimeout(1); I am getting java.net.
> SocketTimeoutException: Read timed out error.
> 
> I think it is not same as ConnectTimeoutException.

No it is not. SO_TIMEOUT is for read, CONNECTION_TIMEOUT for connect.
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/
httpclient/params/HttpConnectionParams.html#CONNECTION_TIMEOUT
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/
httpclient/params/HttpConnectionParams.html#SO_TIMEOUT

hope that helps,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to