KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > http > SimpleHttpSecureClient


1 package org.jahia.clipbuilder.html.web.http;
2
3 import org.org.apache.commons.httpclient.methods.*;
4 import org.org.apache.commons.httpclient.params.*;
5 import org.org.apache.commons.httpclient.*;
6 import java.io.*;
7
8 /**
9  * Description of the Class
10  *
11  *@author Tlili Khaled
12  */

13 public abstract class SimpleHttpSecureClient {
14     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SimpleHttpSecureClient.class);
15
16
17     /**
18      * Gets the ContentAsString attribute of the SimpleHttpSecureClient object
19      *
20      *@param url Description of Parameter
21      *@return The ContentAsString value
22      *@exception Exception Description of Exception
23      */

24     public static String JavaDoc getContentAsString(String JavaDoc url) throws Exception JavaDoc {
25         HttpClient client = new HttpClient();
26
27         // Create a method instance.
28
GetMethod method = new GetMethod(url);
29
30         // Provide custom retry handler is necessary
31
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
32         String JavaDoc content = "";
33
34         // Execute the method.
35
int statusCode = client.executeMethod(method);
36
37         if (statusCode != HttpStatus.SC_OK) {
38             logger.error("[ Method failed: " + method.getStatusLine() + " ]");
39         }
40
41         // Read the response body.
42
byte[] responseBody = method.getResponseBody();
43
44         // Deal with the response.
45
// Use caution: ensure correct character encoding and is not binary data
46
content = new String JavaDoc(responseBody);
47
48         // Release the connection.
49
method.releaseConnection();
50         return content;
51     }
52
53 }
54
Popular Tags