List of usage examples for org.apache.http.client.methods HttpGet HttpGet
public HttpGet(final String uri)
From source file:com.hilatest.httpclient.apacheexample.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("my.keystore")); try {// w w w . j a v a 2 s.com trustStore.load(instream, "nopassword".toCharArray()); } finally { instream.close(); } SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); Scheme sch = new Scheme("https", socketFactory, 443); httpclient.getConnectionManager().getSchemeRegistry().register(sch); HttpGet httpget = new HttpGet("https://localhost/"); 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()); } 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:com.mama100.android.member.http.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.weibo.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }//from w ww . ja v a2 s . co m List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // Consume response content if (entity != null) { entity.consumeContent(); } System.out.println("----------------------------------------"); // 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.hilatest.httpclient.apacheexample.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }//ww w . ja v a 2s. c o m List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // Consume response content if (entity != null) { entity.consumeContent(); } System.out.println("----------------------------------------"); // 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.mtea.macrotea_httpclient_study.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try {/*from w w w . j av a 2 s . c o m*/ // CookieStore CookieStore cookieStore = new BasicCookieStore(); // HttpContext HttpContext localContext = new BasicHttpContext(); //HttpContextCookieStore localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // ? HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } //???cookie List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // EntityUtils.consume(entity); System.out.println("----------------------------------------"); } finally { //??httpclient??? httpclient.getConnectionManager().shutdown(); } }
From source file:org.jaqpot.core.service.client.jpdi.JPDIClientFactory.java
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException { CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create().build(); asyncClient.start();// w w w. java 2 s .c om HttpGet request = new HttpGet("http://www.google.com"); request.addHeader("Accept", "text/html"); Future f = asyncClient.execute(request, new FutureCallback<HttpResponse>() { @Override public void completed(HttpResponse t) { System.out.println("completed"); try { String result = new BufferedReader(new InputStreamReader(t.getEntity().getContent())).lines() .collect(Collectors.joining("\n")); System.out.println(result); } catch (IOException ex) { Logger.getLogger(JPDIClientFactory.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedOperationException ex) { Logger.getLogger(JPDIClientFactory.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void failed(Exception excptn) { System.out.println("failed"); } @Override public void cancelled() { System.out.println("cancelled"); } }); f.get(); asyncClient.close(); }
From source file:eu.transcriptorium.trpclient.ClientAuthentication.java
public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("dbis-faxe.uibk.ac.at", 443), new UsernamePasswordCredentials(user, password)); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try //Set-Cookie: JSESSIONID=ABAD1D;path=/ {/*from w ww. ja v a 2 s . c om*/ HttpGet httpget = new HttpGet("https://dbis-faxe.uibk.ac.at/TrpServerTesting/rest/docs/62/fulldoc.xml"); //httpclient. httpget.setHeader("Cookie", "JSESSIONID=5FB6AC0CD0F4FAC80CE6716DD789F5E8"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:an.dpr.cyclingresultsapi.ClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); StringBuilder ret = new StringBuilder(); try {//w w w. j a v a2 s . c o m //http://cyclingresults-dprsoft.rhcloud.com/rest/competitions/query/20140101,20140601,1,1,UWT // HttpHost target = new HttpHost("cyclingresults-dprsoft.rhcloud.com", 80, "http"); HttpHost proxy = null;// = new HttpHost("proxy.sdc.hp.com", 8080, "http"); HttpHost target = new HttpHost("localhost", 8282, "http"); RequestConfig config = RequestConfig.custom() // .setProxy(proxy) .build(); HttpGet request = new HttpGet("/rest/competitions/query/20130801,20130901,1,1,UWT"); request.setConfig(config); System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); InputStreamReader isr = new InputStreamReader(response.getEntity().getContent(), "cp1252"); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { ret.append(line); } try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } System.out.println(ret.toString()); }
From source file:org.apache.http.examples.client.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); // DefaultHttpParams.getDefaultParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); // httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); try {/*from w ww .ja v a 2s .c o m*/ // HttpGet httpget = new HttpGet("http://localhost/"); HttpGet httpget = new HttpGet( "http://api.map.baidu.com/geocoder/v2/?address=&output=json&ak=E4805d16520de693a3fe707cdc962045&callback=showLocation"); // httpget.setHeader("Accept", "180.97.33.90:80"); httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); httpget.setHeader("Accept-Encoding", "gzip, deflate, sdch"); httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.8"); httpget.setHeader("Cache-Control", "no-cache"); httpget.setHeader("Connection", "keep-alive"); httpget.setHeader("Referer", "http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition"); httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36"); System.out.println("Executing request httpget.getRequestLine() " + httpget.getRequestLine()); // 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(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity, "utf-8") : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); //showLocation&&showLocation({"status":0,"result":{"location":{"lng":112.25009284837,"lat":32.229168591538},"precise":0,"confidence":14,"level":"\u533a\u53bf"}}) System.out.println("----------------------------------------"); ObjectMapper mapper = new ObjectMapper(); // mapper.readValue(responseBody, AddressCoord.class); JsonNode root = mapper.readTree( responseBody.substring("showLocation&&showLocation(".length(), responseBody.length() - 1)); // String name = root.get("name").asText(); // int age = root.get("age").asInt(); String status = root.get("status").asText(); String lng = root.with("result").with("location").get("lng").asText(); String lat = root.with("result").with("location").get("lat").asText(); String level = root.with("result").get("level").asText(); System.out.println(String.format("'%1$s': [%2$s, %3$s], status: %4$s", level, lng, lat, status)); } finally { httpclient.close(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try {//from w ww .j av a 2 s .c o m // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // Consume response content EntityUtils.consume(entity); System.out.println("----------------------------------------"); } 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:cn.anthony.util.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new File("my.keystore"), "nopassword".toCharArray(), new TrustSelfSignedStrategy()).build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try {//from w w w . j a v a 2 s . c o m HttpGet httpget = new HttpGet("https://httpbin.org/"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }