java 发送 http请求——HttpClient

使用HttpClient来发送Http请求

引入两个包:[1]org.apache.httpcomponents.httpclient_x.x.x.jar  [2]org.apache.httpcomponents.httpcore_x.x.x.jar

下载链接:Apache HttpComponents - HttpComponents Downloads

参考文档:[1]HttpClient Tutorial  [2]HttpClient Example


 1 package http;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 import org.apache.http.Header;
 7 import org.apache.http.HttpEntity;
 8 import org.apache.http.ProtocolVersion;
 9 import org.apache.http.StatusLine;
10 import org.apache.http.client.ClientProtocolException;
11 import org.apache.http.client.config.RequestConfig;
12 import org.apache.http.client.methods.CloseableHttpResponse;
13 import org.apache.http.client.methods.HttpGet;
14 import org.apache.http.impl.client.CloseableHttpClient;
15 import org.apache.http.impl.client.HttpClients;
16 
17 
18 
19 public class HttpConnector {
20     public static void main(String[] args) {
21         CloseableHttpClient httpClient = HttpClients.createDefault();
22         RequestConfig requestConfig = RequestConfig.custom()
23                 .setConnectTimeout(1000)
24                 .setSocketTimeout(1000)
25                 .build();
26         HttpGet get = new HttpGet("http://i.easou.com/s.m?q=电影&wver=t");
27         get.setConfig(requestConfig);
28         
29         /*        
30             HttpPost post = new HttpPost("http://i.easou.com/s.m");
31             post.setConfig(requestConfig);
32             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
33             nameValuePairs.add(new BasicNameValuePair("q", "电影"));
34             nameValuePairs.add(new BasicNameValuePair("wver", "t"));
35             UrlEncodedFormEntity urlEntity;
36             urlEntity = new UrlEncodedFormEntity(nameValuePairs,Consts.UTF_8);
37             post.setEntity(urlEntity);
38         */
39         CloseableHttpResponse response = null;
40         try {
41             response = httpClient.execute(get);
42             /*
43                 response = httpClient.execute(post);
44             */
45             StatusLine statusLine = response.getStatusLine();
46             ProtocolVersion protocolVersion = statusLine.getProtocolVersion();
47             String reasonPhrase = statusLine.getReasonPhrase();
48             int statusCode = statusLine.getStatusCode();
49             System.out.println(statusLine + "\n" + protocolVersion + " " + statusCode + " " + reasonPhrase);
50             /*#############################################################################################*/
51             HttpEntity httpEntity = response.getEntity();
52             if(httpEntity != null) {
53                 Header contentType = httpEntity.getContentType();
54                 Header contentEncoding = httpEntity.getContentEncoding();
55                 long contentLength = httpEntity.getContentLength();
56                 boolean isStreaming = httpEntity.isStreaming();
57                 boolean isRepeatable = httpEntity.isRepeatable();
58                 boolean isChunked = httpEntity.isChunked();
59                 System.out.println(contentType + " | " + contentEncoding + " | " + contentLength + " | " + isStreaming + "/" + isRepeatable + "/" + isChunked);
60                 /*#############################################################################################*/
61                 InputStream is = httpEntity.getContent();
62                 byte[] buff = new byte[512];
63                 while(is.read(buff) > 0) {
64                     for(byte b : buff) {
65                         System.out.print((char) b);
66                     }
67                 }
68                 is.close();
69                 /*#############################################################################################*/
70             }
71         } catch (ClientProtocolException e) {
72             e.printStackTrace();
73         } catch (IOException e) {
74             e.printStackTrace();
75         } finally {
76             if(response != null) {
77                 try {
78                     response.close();
79                 } catch (IOException e) {
80                     e.printStackTrace();
81                 }
82             }
83         }
84         try {
85             httpClient.close();
86         } catch (IOException e) {
87             e.printStackTrace();
88         }
89     }
90     
91 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。