hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tomm Krause" <dasKlei...@gmx.net>
Subject Re: handle the request entities from server to client ?
Date Tue, 14 Nov 2006 11:22:09 GMT
Hello Roland,
Thanks a lot for your answer.

The following code worked
fine, but I am not sure if
it is a 'best practice'.


---- servlet source (SERVER)----

// ...
    private void requestMessages(
            String[] messages, HttpServletResponse response) {

        StringRequestEntity s1;
        OutputStream out;
        
        response.setContentType("application/octet-stream");

        try {
            // SEPERATOR is a static final String
            s1 = new StringRequestEntity(
                    Integer.toString(messages.length) + SEPERATOR);
            out = response.getOutputStream();
            s1.writeRequest(out);
            
            for (int i = 0; i < messages.length; i++) {
                
                s1 = new StringRequestEntity(messages[i] + SEPERATOR);    
                out = response.getOutputStream();
                s1.writeRequest(out);    
            }
            
        } catch (UnsupportedEncodingException exc1) {
// ...




---- client source (CLIENT)----

// ...
    * @return Servlet response as <code>String[]</code>
     */
    private String[] getResponse(PostMethod post) {
        
        InputStream is = null;
        String data    = "";
        String[] messages = null;
        
        try {
            
            is  = post.getResponseBodyAsStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader in = new BufferedReader(isr);

            String line;
            while ((line = in.readLine()) != null) {
                data = data + line;
            }

            // SEPERATOR is a static final String
            messages = data.split(SEPERATOR);

        } catch (IOException exc) {
            // handle this later
            exc.printStackTrace();
        } finally {
            post.releaseConnection();
        }

        if (messages == null) {
         // handle this later, throw exception
        }
        
        return messages;
    }


bastian



> Hello Bastian,
> 
> > Is there a little example how I can work with the
> > RequestEntity classes and a servlet.
> 
> You can't. RequestEntity in HttpClient 3.x is a client side
> interface which you can't use on the server side. HttpCore
> allows the use of RequestEntity on the server side, but it
> relies on HttpCore communication primitives which are different
> >from the Servlet API.
> You might find something to clarify the usage of the interfaces
> in the test code for HttpClient 3.x and in the sample code for
> HttpCore 4.0 respectively.
> 
> > I don't know how the data come from a servlet to
> > the HttpClient.
> 
> It is sent over a TCP/IP or SSL connection. The same
> connection over which the request was sent to the server.
> 
> > How will the server wrap the data and how can the
> > client unwrap it ?
> 
> The server writes it's "wrapping" strategy in the
> Transport-Encoding header. The usual options are "id"
> (no header) and "chunked". HttpClient automatically
> interprets the Transport-Encoding header and decodes the
> chunked encoding if necessary. On the client side, you'll
> get the stream as it is sent by the server. Except if
> there is some proxy inbetween that applies a content
> encoding. If that is the case, the Content-Encoding
> header will tell you what encoding has been applied.
> 
> hope that helps,
>   Roland
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org

-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer

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


Mime
View raw message