cancel
Showing results for 
Search instead for 
Did you mean: 

Tech Tip: Reading HTTP responses in a Java Extension

This code snippet illustrates how to read an HTTP response in a Feature Brief: Java Extensions in Stingray Traffic Manager.

 

Reading an HTTP response is not as straightforward as it might appear. Response data may be implicitly empty (for example, 304 Not Modified responses), the content length value may be missing, and the data may be transfer-encoded with chunking.  The response may be compressed by one of several compression algorithms, or may be encrypted using SSL/TLS.

 

Stingray will automatically handle all of this complexity for you - resolving the length of the response, stripping content and transfer encodings and decrypting response data as necessary.The following code snippet will read the response from the server and place the plaintext version in a Java String named 'body':

 

public void doGet( HttpServletRequest req, HttpServletResponse res )

    throws ServletException, IOException

{

    ZXTMHttpServletResponse zres = (ZXTMHttpServletResponse) res;


    // Read the response

    BufferedReader reader = zres.getReader();


    StringBuilder data = new StringBuilder();

    char[] buf = new char[4096];

    int len;

    while( ( len = reader.read( buf ) ) >= 0 ) {

        data.append( buf, 0, len );

    }

    String body = data.toString();


    // Now process the body data...

}

 

Read more

 

Version history
Revision #:
1 of 1
Last update:
‎03-05-2013 08:13:AM
Updated by:
 
Labels (1)