On Fri, Nov 04, 2005 at 08:52:17AM -0800, Guy With Question wrote:
> Oleg,
>
> What I want to do is unblock if I don't have the entire response in x
> seconds, where x = time needed to connect to the server plus the time needed
> to recieve the entire response. Can you please suggest a way to do this? Can
> you show me with an example?
>
> Thanks in advance.
final int SOCKET_TIMEOUT = 1000;
final int TOTAL_TIMEOUT = 5000;
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("/data");
httpget.getParams().setSoTimeout(SOCKET_TIMEOUT);
try {
httpclient.executeMethod(httpget);
InputStream instream = httpget.getResponseBodyAsStream();
int l;
byte[] buffer = new byte[1024];
long start = System.currentTimeMillis();
while ((l = instream.read(buffer)) != -1) {
long time = System.currentTimeMillis() - start;
if (time > TOTAL_TIMEOUT) {
throw new OppsieException();
}
// do stuff
}
} finally {
httpget.releaseConnection();
}
The worst case is TOTAL_TIMEOUT - 1 + SOCKET_TIMEOUT
Hope this helps
Oleg
>
> Oleg Kalnichevski <[EMAIL PROTECTED]> wrote:
> On Thu, Nov 03, 2005 at 02:01:14PM -0800, Guy With Question wrote:
> > Thanks for the clarification.
> >
> > So I guess when I am setting connection timeout = 10, what I'm really doing
> > is setting timeout for the Connection Socket.
> >
> > What I want to do is set timeout for the data Socket as well. If I don't
> > have the complete response within a time period, say 8 seconds (assuming
> > connection timeout is 2 seconds), then I want my client to stop blocking.
> >
>
> Not quite. If you set the socket timeout to, say, 10 sec, and there's a
> packet coming every 9 secs, the connection will never time out. The
> socket timeout only ensures that if there's no data coming FOR 10 sec,
> the socket will stop blocking.
>
> Hope this helps
>
> Oleg
> PS: in the future please post your questions to the mailing list
>
>
> > Regards.
> >
> >
> >
> >
> >
> > On Thu, 2005-11-03 at 11:47 -0800, Guy With Question wrote:
> > > Hello,
> > >
> > > Background:
> > > I am using HttpClient 3.0 rc4. I am trying to connect to an IIS server
> > > using
> > > SSL. I need to POST data to that server. My connection timeout is set to
> > > 10
> > > seconds. When execute method runs, the first response I get is 100
> > > Continue
> > > which is almost immediate, but the HTTP content comes back much after 10
> > > seconds.
> > >
> > > Question:
> > > I am not sure if HttpClient is using a second connection in the execute
> > > method for sending the actual POST data.
> >
> > HttpClient does not use a second connection to execute POST requests
> >
> > > 1. If the client uses the same connection to POST data, then will the
> > > timeout
> > > value NOT matter anymore since the client has already recieved 100
> > > Continue
> > > immediately?
> >
> > The socket timeout defines the maximum period of inactivity between two
> > consecutive incoming IP packets, or in other words the maximum period of
> > time the socket can be blocked in a read operation
> >
> > The connection timeout defines how long the socket can be blocked
> > waiting until the socket is ready to send and receive data. It has no
> > effect on read / write operations
> >
> > > 2. If it uses another connection, then will that connection also have a
> > > connection timeout of 10 seconds. If yes, then why is it taking longer
> > > than
> > > 10 seconds to get my response?
> >
> > See above
> >
> > > 3. If I want to set the connection timeout = 10 seconds from the time the
> > > first request is made to the time I get a final response, what do I have
> > > to
> > > do?
> >
> > It all depends what you mean by the connection timeout. The maximum time
> > until the response is received in its entirety?
> >
> > Hope this helps
> >
> > Oleg
> >
> > >
> > > Code:
> > >
> > > HttpClient httpClient = new HttpClient(new SimpleHttpConnectionManager());
> > >
> > > Integer timeout = new Integer(10*1000);
> > >
> > >
> > >
> > > httpClient.getHttpConnectionManager().getParams().setParameter("http.connection.timeout",
> > > timeout);
> > >
> > > httpPostMethod.setRequestBody(data);
> > >
> > > int statusCode = httpClient.executeMethod(httpPostMethod);
> > >
> > >
> > >
> > > Thanks in advance!
> >
> >
> >
> >
> > ---------------------------------
> > Yahoo! FareChase - Search multiple travel sites in one click.
>
>
> ---------------------------------
> Yahoo! FareChase - Search multiple travel sites in one click.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]