欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)建默認的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)建默認的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)建默認的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)文章

  • MyBatis源碼分析之日志logging詳解

    MyBatis源碼分析之日志logging詳解

    這篇文章主要給大家介紹了關(guān)于MyBatis源碼分析之日志logging的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java實現(xiàn)郵件發(fā)送遇到的問題

    Java實現(xiàn)郵件發(fā)送遇到的問題

    本文給大家分享的是個人在項目過程中,使用Java實現(xiàn)郵件發(fā)送的時候所遇到的幾個問題以及解決方法,有需要的小伙伴可以參考下
    2016-09-09
  • Scala文件操作示例代碼講解

    Scala文件操作示例代碼講解

    本文章向大家介紹Scala 學(xué)習(xí)筆記之文件操作,主要內(nèi)容包括其使用實例、應(yīng)用技巧、基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下
    2023-04-04
  • idea2023創(chuàng)建JavaWeb教程之右鍵沒有Servlet的問題解決

    idea2023創(chuàng)建JavaWeb教程之右鍵沒有Servlet的問題解決

    最近在寫一個javaweb項目,但是在IDEA中創(chuàng)建好項目后,在搭建結(jié)構(gòu)的時候創(chuàng)建servlet文件去沒有選項,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于idea2023創(chuàng)建JavaWeb教程之右鍵沒有Servlet問題的解決方法,需要的朋友可以參考下
    2023-10-10
  • springboot使用kafka事務(wù)的示例代碼

    springboot使用kafka事務(wù)的示例代碼

    Kafka?同數(shù)據(jù)庫一樣支持事務(wù),當發(fā)生異常的時候可以進行回滾,確保消息監(jiān)聽器不會接收到一些錯誤的或者不需要的消息,本文就來介紹一下springboot使用kafka事務(wù)的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • Spring的事務(wù)管理你了解嗎

    Spring的事務(wù)管理你了解嗎

    這篇文章主要為大家介紹了Spring的事務(wù)管理,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 每日六道java新手入門面試題,通往自由的道路--多線程

    每日六道java新手入門面試題,通往自由的道路--多線程

    這篇文章主要為大家分享了最有價值的6道多線程面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對hashCode方法的設(shè)計、垃圾收集的堆和代進行剖析,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Java實現(xiàn)獲取行政區(qū)劃的示例代碼

    Java實現(xiàn)獲取行政區(qū)劃的示例代碼

    這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)獲取行政區(qū)劃的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)游戲
    2023-03-03
  • java實現(xiàn)客戶管理系統(tǒng)

    java實現(xiàn)客戶管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)客戶管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 教你一步解決java.io.FileNotFoundException:找不到文件異常

    教你一步解決java.io.FileNotFoundException:找不到文件異常

    這篇文章主要給大家介紹了關(guān)于如何一步解決java.io.FileNotFoundException:找不到文件異常的相關(guān)資料,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下
    2024-01-01

最新評論