Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.gbrowser.http; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; /** * * @author nusrat */ public class Download { public Download() { } public String getPage(String url) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet; CloseableHttpResponse response1 = null; StringBuilder buff = new StringBuilder(); int ch = -1; try { httpGet = new HttpGet(url.trim()); response1 = httpclient.execute(httpGet); if (response1.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( httpGet.getURI().getPath() + " : " + response1.getStatusLine().getStatusCode() + " : " + response1.getStatusLine().getReasonPhrase()); } System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed // EntityUtils.consume(entity1); InputStream inp = entity1.getContent(); while ((ch = inp.read()) != -1) { buff.append((char) ch); } } finally { if (response1 != null) { response1.close(); } } return buff.toString(); } }