HttpClient4.x工具獲取如何使用

HttpClient4.x工具能夠讓咱們輸入url,就能夠請求某個頁面(我的感受挺實用的,特別是封裝在代碼中)html

首先咱們須要在maven工程中添加依賴apache

<dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>4.5.2</version>
            </dependency>
 
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
                    <dependency>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpcore</artifactId>
            </dependency>
 
 
        <!-- gson工具,封裝http的時候使用 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>
 
接下來,咱們來封裝一下,有get/post請求
 1 /**
 2 * 封裝http get post  3 */
 4 public class HttpUtils {  5 
 6 private static final Gson gson = new Gson();  7 /**
 8 * get方法  9 */
10 public static Map<String,Object> doget(String url,int timeout){ 11 Map<String,Object> map = new HashMap<>(); 12 CloseableHttpClient httpClient = HttpClients.createDefault();
  //配置無關緊要,根據我的情景
13 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) //設置鏈接超時時間 14 .setConnectionRequestTimeout(timeout) //設置請求超時時間 15 .setSocketTimeout(timeout) 16 .setRedirectsEnabled(true) //設置容許自動重定向 17 .build(); 1819 HttpGet httpGet = new HttpGet(url); 20 httpGet.setConfig(requestConfig); 21 22 try { 23 HttpResponse httpResponse = httpClient.execute(httpGet); 24 if(httpResponse.getStatusLine().getStatusCode() == 200){ 25 String jsonResult = EntityUtils.toString(httpResponse.getEntity()); 26 map = gson.fromJson(jsonResult, map.getClass()); //經過gson工具,將響應流轉換成map集合 27 } 28 29 }catch(Exception e){ 30 e.printStackTrace(); 31 }finally{ 32 try{ 33 httpClient.close(); 34 }catch(Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 return map; 39 } 40 /** 41 * 封裝post,與get有殊途同歸之妙 42 */ 43 public static String dopost(String url,String data,int timeout){ 44 CloseableHttpClient httpClient = HttpClients.createDefault(); 45 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) //設置鏈接超時 46 .setConnectionRequestTimeout(timeout) //設置請求超時 47 .setRedirectsEnabled(true) //設置容許自動重定向 48 .build(); 49 50 HttpPost httpPost = new HttpPost(url); 51 httpPost.setConfig(requestConfig); 52 httpPost.addHeader("Content-Type","text/html; chartset=UTF-8"); //添加頭信息 53 if (data != null && data instanceof String) {//使用字符串傳參 54 StringEntity stringEntity = new StringEntity(data, "UTF-8"); 55 httpPost.setEntity(stringEntity); 56 } 57 try { 58 CloseableHttpResponse httpResponse = httpClient.execute(httpPost); 59 HttpEntity httpEntity = httpResponse.getEntity(); 60 if (httpResponse.getStatusLine().getStatusCode() == 200){ 61 String result = EntityUtils.toString(httpEntity); 62 return result; 63 } 64 } catch (Exception e) { 65 e.printStackTrace(); 66 }finally { 67 try{ 68 httpClient.close(); 69 }catch (Exception e){ 70 e.printStackTrace(); 71 } 72 } 73 74 return null; 75 76 } 77 }

封裝好HttpClient工具後,咱們來調用一下json

get請求maven

HttpUtils.doget(請求地址,請求時間(timeout));

post請求工具

HttpUtils.dopost(請求地址, 請求傳的數據(傳以前須要轉換成string), 請求時間(timeout));
相關文章
相關標籤/搜索