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 :

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




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) {

			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("*** Response headers ***").append("<br/>");
			Header[] headers = res.getAllHeaders();
			for (Header header : headers) {

		} 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"
<td width="144"><strong>Website URL</strong></td>
<td width="8">&nbsp;</td>
<td width="203"><label> <input type="text" name="inputURL" />
<td>(example :</td>
<td><label> <input type="submit" name="GET" value="GET" class="button" />


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 :



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

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.