List of usage examples for org.apache.http.client.methods HttpGet HttpGet
public HttpGet(final String uri)
From source file:demo.example.ClientAbortMethod.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*w ww . j a v a 2 s . c o m*/ HttpGet httpget = new HttpGet("http://httpbin.org/get"); System.out.println("Executing request " + httpget.getURI()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Do not feel like reading the response body // Call abort on the request object httpget.abort(); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.hilatest.httpclient.apacheexample.ClientAbortMethod.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://www.apache.org/"); System.out.println("executing request " + httpget.getURI()); 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()); }//from ww w.ja v a 2s . co m System.out.println("----------------------------------------"); // Do not feel like reading the response body // Call abort on the request object httpget.abort(); // 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.lxf.spider.client.ClientAbortMethod.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*w w w. j a v a 2s.c o m*/ HttpGet httpget = new HttpGet("http://www.apache.org/"); System.out.println("Executing request " + httpget.getURI()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Do not feel like reading the response body // Call abort on the request object httpget.abort(); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientAbortMethod.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try {/* ww w . ja v a 2 s. co m*/ HttpGet httpget = new HttpGet("http://www.apache.org/"); System.out.println("executing request " + httpget.getURI()); 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()); } System.out.println("----------------------------------------"); // Do not feel like reading the response body // Call abort on the request object httpget.abort(); } 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.oeg.oops.Main.java
public static void main(String[] args) { System.out.println("Hello, World"); for (int i = 0; i < args.length; i++) { System.out.println("arg: " + args[i]); }//from w w w . j av a2 s.co m // if(args.length<2){ // System.out.println("expect 2 arguments: vocabulary-uri and output-directory"); // } // else{ // String uri = args[0]; // String output_dir = args[1]; if (true) { String uri = "https://raw.githubusercontent.com/ahmad88me/demo/master/alo.owl"; String output_dir = "./output"; System.out.println("uri: " + uri); System.out.println("output dir: " + output_dir); try { HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(uri); request.setHeader("Accept", "application/rdf+xml"); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == 200) { String contentType = response.getFirstHeader("Content-Type").getValue(); System.out.println("content type: " + contentType); } } catch (Exception e) { System.err.println("Error while doing http get: " + " in " + uri + " " + e.getMessage()); } //Vocabulary v = new Vocabulary(uri); // CreateOOPSEvalPage coep = new CreateOOPSEvalPage(v); // coep.createPage(output_dir); } }
From source file:com.mtea.macrotea_httpclient_study.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try {// w ww. j a v a2s. c o m HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // ResponseHandler ResponseHandler<String> responseHandler = new BasicResponseHandler(); //???BasicResponseHandler String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { //??httpclient??? httpclient.getConnectionManager().shutdown(); } }
From source file:com.dreamer.QuickStart.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w ww .ja v a2s .c om HttpGet httpGet = new HttpGet("http://localhost:8080"); 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 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:jdbc.ClientFormLogin.java
public static void main(String[] args) throws Exception { BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); java.net.CookieManager cm = new java.net.CookieManager(); java.net.CookieHandler.setDefault(cm); try {/*from ww w. j av a 2s . co m*/ HttpGet httpget = new HttpGet("http://www.cophieu68.vn/export/excel.php?id=AAA"); CloseableHttpResponse response1 = httpclient.execute(httpget); try { HttpEntity entity = response1.getEntity(); System.out.println("Login form get: " + response1.getStatusLine()); EntityUtils.consume(entity); System.out.println("Initial set of cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response1.close(); } HttpUriRequest login = RequestBuilder.post() .setUri(new URI("http://www.cophieu68.vn/export/excel.php?id=AAA")) .addParameter("username", "hello_nguyenson@live.com").addParameter("tpassword", "19931994") .build(); CloseableHttpResponse response2 = httpclient.execute(login); try { HttpEntity entity = response2.getEntity(); System.out.println("Login form get: " + response2.getStatusLine()); EntityUtils.consume(entity); System.out.println("Post logon cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:baseFrame.netty.atest.QuickTest.java
public static void main(String[] args) throws Exception { for (int i = 0; i < 100000; i++) { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w .j a v a2 s .c o m HttpGet httpGet = new HttpGet("http://127.0.0.1:9990"); CloseableHttpResponse response1 = httpclient.execute(httpGet); long startTime = System.nanoTime(); System.out.println(response1.getStatusLine()); HttpEntity entity = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed // System.out.println(entity.getContentType()); long endTime = System.nanoTime(); System.out.println("-----" + (endTime - startTime)); } finally { httpclient.close(); } } /* CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet("http://127.0.0.1:8080"); 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 entity = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed // System.out.println(entity.getContentType()); //?? System.out.println(entity.getContentEncoding()); // System.out.println(entity.getContentLength()); //? System.out.println(EntityUtils.toString(entity)); //? //System.out.println(EntityUtils.toByteArray(entity).length); //? //entity.getContent(); EntityUtils.consume(entity); } 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.mtea.macrotea_httpclient_study.ClientAbortMethod.java
public final static void main(String[] args) throws Exception { //? DefaultHttpClient HttpClient httpclient = new DefaultHttpClient(); try {//from w ww. j a v a2s . c o m HttpGet httpget = new HttpGet("http://www.baidu.com/"); System.out.println("executing request " + httpget.getURI()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println("RequestLine:" + httpget.getRequestLine()); System.out.println("RequestLine:" + response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } System.out.println("----------------------------------------"); // EntityUtils.consume(entity); //??response?httpget httpget.abort(); } catch (Exception e) { e.printStackTrace(); } finally { //??httpclient??? httpclient.getConnectionManager().shutdown(); } }