List of usage examples for org.apache.http.client.methods HttpGet HttpGet
public HttpGet(final String uri)
From source file:com.lxf.spider.client.ClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w. j a va 2 s . c o m HttpHost target = new HttpHost("localhost", 443, "https"); HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("/"); request.setConfig(config); System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:servletPackage.ClientAuthentication.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); HttpGet httpget = new HttpGet( "http://localhost:8080/alfresco/d/a/workspace/SpacesStore/e80fb2c9-8468-45cd-b943-2d76ae13a260/epl-v10.html"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }/* w w w .jav a 2 s .c o m*/ if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:myexamples.QuickStart.java
public static void main(String[] args) throws Exception { Gson gson = new Gson(); CloseableHttpClient httpclient = HttpClients.createDefault(); try {// ww w . j a v a2s. c o m HttpGet httpGet = new HttpGet("http://localhost:9200/gb/tweet/9"); CloseableHttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally clause. // Please note that if response content is not fully consumed the underlying // connection cannot be safely re-used and will be shut down and discarded // by the connection manager. try { System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < 2048) { System.out.println(EntityUtils.toString(entity1)); } else { System.out.println("entity length=" + len); } } EntityUtils.consume(entity1); } finally { response1.close(); } /* HttpPost httpPost = new HttpPost("http://targethost/login"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine()); HttpEntity entity2 = response2.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response2.close(); } */ } finally { httpclient.close(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientAuthentication.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); try {// ww w. j a v a2 s . c o m httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); HttpGet httpget = new HttpGet("https://localhost/protected"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.liferay.mobile.sdk.BuilderAntTask.java
public static void main(String[] args) { Map<String, String> arguments = parseArguments(args); String url = arguments.get("url"); String context = arguments.get("context"); String filter = arguments.get("filter"); StringBuilder sb = new StringBuilder(); sb.append(url);/*from ww w.j a v a2s.co m*/ if (Validator.isNotNull(context)) { sb.append("/"); sb.append(context); } sb.append("/api/jsonws?discover"); if (Validator.isNull(filter)) { sb.append("=/*"); } else { sb.append("=/"); sb.append(filter); sb.append("/*"); } HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(sb.toString()); DiscoveryResponseHandler handler = new DiscoveryResponseHandler(); try { String builderType = arguments.get("builder"); Builder builder = null; if (builderType.equals("android")) { builder = new AndroidBuilder(); } Discovery discovery = client.execute(get, handler); PortalVersion version = HttpUtil.getPortalVersion(url); if (Validator.isNull(filter)) { builder.buildAll(version, discovery); } else { builder.build(filter, version, discovery); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.boonya.http.async.examples.nio.client.AsyncClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {//from www . j av a2 s . com httpclient.start(); HttpHost proxy = new HttpHost("someproxy", 8080); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("https://issues.apache.org/"); request.setConfig(config); Future<HttpResponse> future = httpclient.execute(request, null); HttpResponse response = future.get(); System.out.println("Response: " + response.getStatusLine()); System.out.println("Shutting down"); } finally { httpclient.close(); } }
From source file:com.http.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from www .j ava2 s. c o m HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); System.out.println(response.getStatusLine()); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); entity = new BufferedHttpEntity(entity); System.out.println("---1-----:" + EntityUtils.toString(entity)); System.out.println("---2-----:" + EntityUtils.toString(entity)); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { httpclient.close(); } }
From source file:com.hilatest.httpclient.apacheexample.ClientAuthentication.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); HttpGet httpget = new HttpGet("https://localhost/protected"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }// w w w . j a v a 2 s .c o m if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:httpasync.AsyncClientPipelined.java
public static void main(final String[] args) throws Exception { CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); try {/*from ww w. j a v a 2s. c o m*/ httpclient.start(); HttpHost targetHost = new HttpHost("money.moneydj.com", 80); HttpGet[] resquests = { new HttpGet("/us/basic/basic0001/Person"), new HttpGet("/us/basic/basic0001/AA"), new HttpGet("/us/basic/basic0001/PSG"), // new HttpGet("/docs/introduction.html"), // new HttpGet("/docs/setup.html"), // new HttpGet("/docs/config/index.html") }; Future<List<HttpResponse>> future = httpclient.execute(targetHost, Arrays.<HttpRequest>asList(resquests), null); List<HttpResponse> responses = future.get(); responses.forEach(System.out::println); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:com.orange.ClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w w w. j a v a2 s. co m HttpHost target = new HttpHost("httpbin.org", 443, "https"); HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("/"); request.setConfig(config); System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); } finally { response.close(); } } finally { httpclient.close(); } }