最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

HttpClient實現(xiàn)調(diào)用外部項目接口工具類的示例

 更新時間:2017年10月07日 10:09:06   作者:小曉峰  
下面小編就為大家?guī)硪黄狧ttpClient實現(xiàn)調(diào)用外部項目接口工具類的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

實例如下:

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpUtils {
 private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000)
  .setConnectionRequestTimeout(15000).build();

 public static String sendHttpGet(HttpGet httpGet) {
 CloseableHttpClient httpClient = null;
 CloseableHttpResponse response = null;
 HttpEntity entity = null;
 String responseContent = null;
 try {
  // 創(chuàng)建默認(rèn)的httpClient實例.
  httpClient = HttpClients.createDefault();
  httpGet.setConfig(requestConfig); 
  
  // 執(zhí)行請求
  response = httpClient.execute(httpGet);
  entity = response.getEntity();
  responseContent = EntityUtils.toString(entity, "UTF-8");
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  try {
  // 關(guān)閉連接,釋放資源
  if (response != null) {
   response.close();
  }
  if (httpClient != null) {
   httpClient.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 return responseContent;
 }
 /** 
   * 發(fā)送 post請求 
   * @param httpUrl 地址 
   * @param maps 參數(shù) 
   */ 
  public static String sendHttpPost(String httpUrl, Map<String, String> maps) { 
    HttpPost httpPost = new HttpPost(httpUrl);// 創(chuàng)建httpPost  
    // 創(chuàng)建參數(shù)隊列  
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    for (String key : maps.keySet()) { 
      nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); 
    } 
    try { 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return sendHttpPost(httpPost); 
  } 
   
   
 public static String sendHttpPost(HttpPost httpPost) {
 CloseableHttpClient httpClient = null;
 CloseableHttpResponse response = null;
 HttpEntity entity = null;
 String responseContent = null;
 try {
  // 創(chuàng)建默認(rèn)的httpClient實例.
  httpClient = HttpClients.createDefault();
  httpPost.setConfig(requestConfig);
  // 執(zhí)行請求
  response = httpClient.execute(httpPost);
  entity = response.getEntity();
  responseContent = EntityUtils.toString(entity, "UTF-8");
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  try {
  // 關(guān)閉連接,釋放資源
  if (response != null) {
   response.close();
  }
  if (httpClient != null) {
   httpClient.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 return responseContent;
 }
 
 /** 
   * 發(fā)送Get請求Https 
   * @param httpPost 
   * @return 
   */ 
  public static String sendHttpsGet(HttpGet httpGet) { 
    CloseableHttpClient httpClient = null; 
    CloseableHttpResponse response = null; 
    HttpEntity entity = null; 
    String responseContent = null; 
    try { 
      // 創(chuàng)建默認(rèn)的httpClient實例. 
      PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString())); 
      DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); 
      httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); 
      httpGet.setConfig(requestConfig); 
      // 執(zhí)行請求 
      response = httpClient.execute(httpGet); 
      entity = response.getEntity(); 
      responseContent = EntityUtils.toString(entity, "UTF-8"); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        // 關(guān)閉連接,釋放資源 
        if (response != null) { 
          response.close(); 
        } 
        if (httpClient != null) { 
          httpClient.close(); 
        } 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return responseContent; 
  } 
}

以上這篇HttpClient實現(xiàn)調(diào)用外部項目接口工具類的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳細(xì)分析java并發(fā)之volatile關(guān)鍵字

    詳細(xì)分析java并發(fā)之volatile關(guān)鍵字

    這篇文章主要介紹了java并發(fā)之volatile關(guān)鍵字的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 教你怎么用Java實現(xiàn)給圖片打上水印

    教你怎么用Java實現(xiàn)給圖片打上水印

    這篇文章主要介紹了教你怎么用Java實現(xiàn)給圖片打上水印,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 理解Java面向?qū)ο缶幊淘O(shè)計

    理解Java面向?qū)ο缶幊淘O(shè)計

    這篇文章主要介紹了理解Java面向?qū)ο缶幊淘O(shè)計,面向?qū)ο缶幊淌且环N編程思維方式和編碼架構(gòu)。下面詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-01-01
  • Java實現(xiàn)訂單超時自動取消的7種方案

    Java實現(xiàn)訂單超時自動取消的7種方案

    在電商、外賣、票務(wù)等系統(tǒng)中,訂單超時未支付自動取消是一個常見的需求,這個功能乍一看很簡單,甚至很多初學(xué)者會覺得:"不就是加個定時器么?" 但真到了實際工作中,細(xì)節(jié)的復(fù)雜程度往往會超乎預(yù)期,本文給大家介紹了Java實現(xiàn)訂單超時自動取消的7種方案
    2024-12-12
  • Java單測void類型的方法詳解

    Java單測void類型的方法詳解

    這篇文章主要給大家介紹了Java中單測void類型的方法,文中給出了詳細(xì)的示例代碼,相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價值,需要的朋友可以跟著小編下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • Java對Excel表格的上傳和下載處理方法

    Java對Excel表格的上傳和下載處理方法

    這篇文章主要介紹了Java對Excel表格的上傳和下載處理方法,需要的朋友可以參考下
    2017-08-08
  • Java中BigDecimal類的使用詳解

    Java中BigDecimal類的使用詳解

    這篇文章主要介紹了Java中BigDecimal類的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • vue+springboot讀取git的markdown文件并展示功能

    vue+springboot讀取git的markdown文件并展示功能

    Markdown-it 是一個用于解析和渲染 Markdown 標(biāo)記語言的 JavaScript 庫,使用 Markdown-it,你可以將 Markdown 文本解析為 HTML 輸出,并且可以根據(jù)需要添加功能、擴(kuò)展語法或修改解析行為,本文介紹vue+springboot讀取git的markdown文件并展示,感興趣的朋友一起看看吧
    2024-01-01
  • Spring?Security?自定義授權(quán)服務(wù)器實踐記錄

    Spring?Security?自定義授權(quán)服務(wù)器實踐記錄

    授權(quán)服務(wù)器(Authorization Server)目前并沒有集成在Spring Security項目中,而是作為獨立項目存在于Spring生態(tài)中,這篇文章主要介紹了Spring?Security?自定義授權(quán)服務(wù)器實踐,需要的朋友可以參考下
    2022-08-08
  • MyBatis傳入List集合查詢數(shù)據(jù)問題

    MyBatis傳入List集合查詢數(shù)據(jù)問題

    這篇文章主要介紹了MyBatis傳入List集合查詢數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論

登封市| 湖口县| 沂南县| 元江| 平谷区| 民县| 锦屏县| 东莞市| 崇左市| 宜城市| 合江县| 稻城县| 汨罗市| 汝城县| 新乡县| 丰原市| 天峻县| 云阳县| 响水县| 盐山县| 政和县| 赤壁市| 泰州市| 阿拉善盟| 耒阳市| 阳朔县| 曲阳县| 定远县| 衡阳县| 甘孜| 湖州市| 珲春市| 铁岭市| 醴陵市| 如东县| 丰台区| 贵港市| 文山县| 阿拉善盟| 胶南市| 新源县|