Using HTTPClient with Codenvy

The HttpClient library is very powerful, enabling you to perform almost any HTTP task such as GETs, POSTs, getting and setting cookies, handling redirects, going through HTTP proxies, and even HTTPS authentication. Here we use HTTPClient with GET for a given URL to fetch basic information (Headers, cookies, servers etc.,)

Create a Project

Login to your codenvy workspace and create a New WAR Project say “SimpleGET”. Project file structure is shown below :
project_structure

Create pom.xml and specify dependencies

Specify the below dependencies in pom.xml which are prerequisite for the HTTPClient implementation. Make sure these jars are included under Maven Dependencies:

  • httpclient-4.2.5.jar
  • Javaee-web-api-6.0.jar

Pom.xml:

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

Create SimpleGet Class

This servlet process the given URL and fetches all the header, status information through GET method using httpclient

Code Logic :

private HttpClient client = new DefaultHttpClient();
	private final String USER_AGENT = "Mozilla/5.0";
protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		StringBuffer sb = null;
		try {
			String getURL = request.getParameter("inputURL");

			HttpGet req = new HttpGet(getURL);
			// add request header
			req.addHeader("User-Agent", USER_AGENT);
			HttpResponse res = client.execute(req);
	        HttpEntity entity = res.getEntity();

	        if (entity != null) {
	            EntityUtils.consume(entity);
	        }

			sb = new StringBuffer("<br/>Simple GET request to url : "+ getURL +"<br/>");
			sb.append("*** Request headers ***").append("<br/>");
			Header[] requestHeaders = req.getAllHeaders();
			for (Header header : requestHeaders) {
				sb.append(header.toString()).append("<br/>");
			}
			sb.append("***********************").append("<br/>");

			sb.append("*** Response headers ***").append("<br/>");
			sb.append(res.getStatusLine()).append("<br/>");
			Header[] headers = res.getAllHeaders();
			for (Header header : headers) {
				sb.append(header.toString()).append("<br/>");
			}

		} catch (Exception ex) {
			sb = new StringBuffer(ex.toString());
		}

  		request.setAttribute("data", sb.toString());
  		RequestDispatcher rd = request.getRequestDispatcher("/success.jsp");
  		rd.forward(request, response);
	}

Create index.jsp

Index jsp acts as an interface to input the URL of a webpage for which all the header,status information is captured in SimpleGET class and presented through success.jsp.
Index.jsp form :

<form action="SimpleGET" method="post">
<table width="400" border="0" cellspacing="0" cellpadding="0"
class="table">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="144"><strong>Website URL</strong></td>
<td width="8">&nbsp;</td>
<td width="203"><label> <input type="text" name="inputURL" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>(example : http://www.codenvy.com)</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><label> <input type="submit" name="GET" value="GET" class="button" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>

simple_get

Create web.xml and success.jsp

We are mapping the index.jsp file to the earlier created SimpleGET class in this web.xml file and the action will load the success.jsp:
Web.xml :

<web-app>
<display-name>httpClientCodenvy</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<display-name>SimpleGET</display-name>
<servlet-name>SimpleGET</servlet-name>
<servlet-class>com.codenvy.SimpleGET</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleGET</servlet-name>
<url-pattern>/SimpleGET</url-pattern>
</servlet-mapping>
</web-app>

Success.jsp:

<body>
<div class=CodenvyDiv>
<p>Data : <br/><%=request.getAttribute("data") %></p>
<br/>
<p><a href="index.jsp">Try</a> another url</p>
</div>
</body>

Build and Run the application

Upon successful building the application and executing, it loads index.jsp where we input a URL, then it will be successfully fetch the information on given URL through GET request and displays on screen as success.jsp.
simple_get_output