[httpclient]

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[httpclient]

Trevor Miller-2
Hi All,

I'm using HttpClient to download a simple XML document from a jsp
page, I want to be able to show a progress bar if it takes a long
time.

In order to test this, i write a simple JSP with some calls to
Thread.sleep to simulate network latency and used the following code:

        HttpClient client = new HttpClient();
        HttpMethod method = new
GetMethod("http://localhost:8084/TestHTTPWeb/index.jsp");
        try {
            client.executeMethod (method);
            System.out.println ("Connected, reading response.");
            InputStream in = new BufferedInputStream(
                          new ProgressMonitorInputStream(
                                  this,
                                  "Reading",
                                  method.getResponseBodyAsStream ()));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

Needless to say, this does not work since by the time
method.getResponseBodyAsStream () is called, the data has already been
transferred.

Is there a way to do this?

Regards,
Trevor Miller

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [httpclient]

Christian Hufgard
Hi Trevor

> Needless to say, this does not work since by the time
> method.getResponseBodyAsStream () is called, the data has already been
> transferred.

I do not think so. This is the preferred way a response should be
read, since it does _not_ buffer the whole response in memory.

Are you shure that this code does not work? How do yo figure out the
overall response size?


Cheers,


Chris


Wednesday, August 9, 2006, 11:13:45 AM, you wrote:

> Hi All,

> I'm using HttpClient to download a simple XML document from a jsp
> page, I want to be able to show a progress bar if it takes a long
> time.

> In order to test this, i write a simple JSP with some calls to
> Thread.sleep to simulate network latency and used the following code:

>         HttpClient client = new HttpClient();
>         HttpMethod method = new
> GetMethod("http://localhost:8084/TestHTTPWeb/index.jsp");
>         try {
>             client.executeMethod (method);
>             System.out.println ("Connected, reading response.");
>             InputStream in = new BufferedInputStream(
>                           new ProgressMonitorInputStream(
>                                   this,
>                                   "Reading",
>                                   method.getResponseBodyAsStream ()));
>         } catch (IOException ex) {
>             ex.printStackTrace();
>         }

> Needless to say, this does not work since by the time
> method.getResponseBodyAsStream () is called, the data has already been
> transferred.

> Is there a way to do this?

> Regards,
> Trevor Miller

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail:
> [hidden email]



--
Best regards,
 Christian                            mailto:[hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [httpclient]

Trevor Miller-2
Hi Chris,

Trust me, this does not work. I have written a Servlet with the following code:

    protected void processRequest (HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType ("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter ();
        out.println("<events>");
        for (int i = 0; i < 100; i++) {
            out.println ("<event title=\"Event 5\"
time=\"200609131430\" id=\"5\">new Event</event>");
            try {
                Thread.sleep (500);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        out.println("</events>");
        out.close ();
    }

Which should take close on 50 seconds to execute.

According to ProgressMonitorInutStream javadocs:
If its taking a while, a progress dialog will be popped up to inform the user.

My client application runs and after 53 seconds it prints out:
Connected, reading response.

Which indicates to me that all the data is read before it gets the
input stream. I guess I'm going to have to go look at the code for
HTTPClient....

Regards,
Trevor

On 8/9/06, Christian Hufgard <[hidden email]> wrote:

> Hi Trevor
>
> > Needless to say, this does not work since by the time
> > method.getResponseBodyAsStream () is called, the data has already been
> > transferred.
>
> I do not think so. This is the preferred way a response should be
> read, since it does _not_ buffer the whole response in memory.
>
> Are you shure that this code does not work? How do yo figure out the
> overall response size?
>
>
> Cheers,
>
>
> Chris
>
>
> Wednesday, August 9, 2006, 11:13:45 AM, you wrote:
>
> > Hi All,
>
> > I'm using HttpClient to download a simple XML document from a jsp
> > page, I want to be able to show a progress bar if it takes a long
> > time.
>
> > In order to test this, i write a simple JSP with some calls to
> > Thread.sleep to simulate network latency and used the following code:
>
> >         HttpClient client = new HttpClient();
> >         HttpMethod method = new
> > GetMethod("http://localhost:8084/TestHTTPWeb/index.jsp");
> >         try {
> >             client.executeMethod (method);
> >             System.out.println ("Connected, reading response.");
> >             InputStream in = new BufferedInputStream(
> >                           new ProgressMonitorInputStream(
> >                                   this,
> >                                   "Reading",
> >                                   method.getResponseBodyAsStream ()));
> >         } catch (IOException ex) {
> >             ex.printStackTrace();
> >         }
>
> > Needless to say, this does not work since by the time
> > method.getResponseBodyAsStream () is called, the data has already been
> > transferred.
>
> > Is there a way to do this?
>
> > Regards,
> > Trevor Miller
>
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail:
> > [hidden email]
>
>
>
> --
> Best regards,
>  Christian                            mailto:[hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
Real programmers use Linux!

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re[2]: [httpclient]

Christian Hufgard
<snip>
>         for (int i = 0; i < 100; i++) {
>             out.println ("<event title=\"Event 5\"
> time=\"200609131430\" id=\"5\">new Event</event>");
>             try {
>                 Thread.sleep (500);
>             } catch (InterruptedException ex) {
>                 ex.printStackTrace();
>             }
> Which should take close on 50 seconds to execute.
</snip>

Oh, I had this problem once, too. You should flush the jsps
outputstream, otherwise the reponse is cached until the buffer is
filled.

Chris


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]