我们在开发过程中经常会使用到HTTP协议作为我们数据交换的协议,本小节主要介绍一个开源的HTTP组件的使用。
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpcomponents.version}</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>${httpcomponents.version}</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>${httpcomponents.version}</version> </dependency>
public void simpleGet() { HttpGet get = new HttpGet("http://www.baidu.com"); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(get);) { handleResponse(response); } catch (IOException e) { e.printStackTrace(); } }
public void simplePost() { HttpPost post = new HttpPost("http://www.baidu.com"); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(post);) { handleResponse(response); } catch (IOException e) { e.printStackTrace(); } }
private void handleResponse(HttpResponse response) throws IOException { int statusCode = 0; if ((statusCode = response.getStatusLine().getStatusCode()) == 200) { HttpEntity entity = response.getEntity(); System.out.println(entity.getContentType()); System.out.println(entity.getContentEncoding()); System.out.println(entity.getContentLength()); try (InputStream ins = response.getEntity().getContent();) { BufferedReader reader = new BufferedReader( new InputStreamReader(ins)); String str = null; while ((str = reader.readLine()) != null) { System.out.println(str); } } } else { System.out.println("------------http error: statusCode is: " + statusCode); } }
public void simpleURIGet() throws URISyntaxException { URI uri = new URIBuilder().setScheme("http").setHost("www.google.com") .setPath("/search").setParameter("q", "httpclient") .setParameter("btnG", "Google Search").setParameter("aq", "f") .setParameter("oq", "").build(); HttpGet get = new HttpGet(uri); System.out.println(get.getURI()); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(get);) { handleResponse(response); } catch (IOException e) { e.printStackTrace(); } }
public void simpleURIPost() throws URISyntaxException { URI uri = new URIBuilder().setScheme("http").setHost("www.google.com") .setPath("/search").setParameter("q", "httpclient") .setParameter("btnG", "Google Search").setParameter("aq", "f") .setParameter("oq", "").build(); HttpPost post = new HttpPost(uri); System.out.println(post.getURI()); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(post);) { handleResponse(response); } catch (IOException e) { e.printStackTrace(); } }
public void simpleParamPost() throws UnsupportedEncodingException { HttpPost post = new HttpPost("http://www.baidu.com"); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("name", "zhangsan")); params.add(new BasicNameValuePair("password", "1234")); post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(post);) { handleResponse(response); } catch (IOException e) { e.printStackTrace(); } }
public void addHeadersGet() throws UnsupportedEncodingException { HttpGet get = new HttpGet("http://www.baidu.com"); get.setHeader("User-Agent", "Ahopedog/5.0 (Linux NT 5.1; rv:5.0) Gecko/20100101 FireDog/5.0"); get.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("name", "zhangsan")); params.add(new BasicNameValuePair("password", "1234")); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(get);) { handleResponse(response); } catch (IOException e) { e.printStackTrace(); } }
public void addHeadersPost() throws UnsupportedEncodingException { HttpPost post = new HttpPost("http://www.baidu.com"); post.setHeader("User-Agent", "Ahopedog/5.0 (Linux NT 5.1; rv:5.0) Gecko/20100101 FireDog/5.0"); post.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("name", "zhangsan")); params.add(new BasicNameValuePair("password", "1234")); post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(post);) { handleResponse(response); } catch (IOException e) { e.printStackTrace(); } }
public void handlePostResponseHeader() throws UnsupportedEncodingException { HttpPost post = new HttpPost("http://www.baidu.com"); try (CloseableHttpClient httpClient = new DefaultHttpClient(); CloseableHttpResponse response = httpClient.execute(post);) { Header[] headers = response.getAllHeaders(); for (Header h : headers) { System.out.println(h.getName() + " : " + h.getValue()); } Header serverHeader = response.getFirstHeader("server"); System.out.println(serverHeader.getName() + " : " + serverHeader.getValue()); } catch (IOException e) { e.printStackTrace(); } }
public void simpleGet() { Request request = Request.Get("http://www.baidu.com"); handleContent(request); }
public void simplePost() { Request request = Request.Post("http://www.baidu.com"); handleContent(request); }
private void handleContent(Request request) { Response response; try { response = request.execute(); System.out.println(response.returnContent()); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public void simpleURIPost() { URI uri = URI.create("http://www.sina.com"); Request request = Request.Post(uri).bodyForm( Form.form().add("username", "zhangsan").add("pasword", "pwd") .build()); handleContent(request); }
public void simpleHeaderPost() { Request request = Request.Post("http://www.sina.com.cn"); request.addHeader("User-Agent", "Ahopedog/5.0 (Linux NT 5.1; rv:5.0) Gecko/20100101 FireDog/5.0"); request.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); handleResponse(request); handleEntity(request); } private void handleEntity(Request request) { try { HttpResponse response = request.execute().returnResponse(); HttpEntity entity = response.getEntity(); Header header = entity.getContentType(); System.out.println(header.getName() + ": " + header.getValue()); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void handleResponse(Request request) { try { HttpResponse response = request.execute().returnResponse(); Header[] headers = response.getAllHeaders(); for (Header header : headers) { System.out.println("Header Name: " + header.getName() + "; Header Value: " + header.getValue()); } } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }