HttpClient实现调用外部项目接口工具类的示例

 更新时间:2017年10月07日 10:09:06   作者:小晓峰  
下面小编就为大家带来一篇HttpClient实现调用外部项目接口工具类的示例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

实例如下:

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 {
  // 创建默认的httpClient实例.
  httpClient = HttpClients.createDefault();
  httpGet.setConfig(requestConfig); 
  
  // 执行请求
  response = httpClient.execute(httpGet);
  entity = response.getEntity();
  responseContent = EntityUtils.toString(entity, "UTF-8");
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  try {
  // 关闭连接,释放资源
  if (response != null) {
   response.close();
  }
  if (httpClient != null) {
   httpClient.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 return responseContent;
 }
 /** 
   * 发送 post请求 
   * @param httpUrl 地址 
   * @param maps 参数 
   */ 
  public static String sendHttpPost(String httpUrl, Map<String, String> maps) { 
    HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost  
    // 创建参数队列  
    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 {
  // 创建默认的httpClient实例.
  httpClient = HttpClients.createDefault();
  httpPost.setConfig(requestConfig);
  // 执行请求
  response = httpClient.execute(httpPost);
  entity = response.getEntity();
  responseContent = EntityUtils.toString(entity, "UTF-8");
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  try {
  // 关闭连接,释放资源
  if (response != null) {
   response.close();
  }
  if (httpClient != null) {
   httpClient.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 return responseContent;
 }
 
 /** 
   * 发送Get请求Https 
   * @param httpPost 
   * @return 
   */ 
  public static String sendHttpsGet(HttpGet httpGet) { 
    CloseableHttpClient httpClient = null; 
    CloseableHttpResponse response = null; 
    HttpEntity entity = null; 
    String responseContent = null; 
    try { 
      // 创建默认的httpClient实例. 
      PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString())); 
      DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); 
      httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); 
      httpGet.setConfig(requestConfig); 
      // 执行请求 
      response = httpClient.execute(httpGet); 
      entity = response.getEntity(); 
      responseContent = EntityUtils.toString(entity, "UTF-8"); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        // 关闭连接,释放资源 
        if (response != null) { 
          response.close(); 
        } 
        if (httpClient != null) { 
          httpClient.close(); 
        } 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return responseContent; 
  } 
}

以上这篇HttpClient实现调用外部项目接口工具类的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详细分析java并发之volatile关键字

    详细分析java并发之volatile关键字

    这篇文章主要介绍了java并发之volatile关键字的的相关资料,文中代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • 教你怎么用Java实现给图片打上水印

    教你怎么用Java实现给图片打上水印

    这篇文章主要介绍了教你怎么用Java实现给图片打上水印,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • 理解Java面向对象编程设计

    理解Java面向对象编程设计

    这篇文章主要介绍了理解Java面向对象编程设计,面向对象编程是一种编程思维方式和编码架构。下面详细内容,需要的小伙伴可以参考一下
    2022-01-01
  • Java实现订单超时自动取消的7种方案

    Java实现订单超时自动取消的7种方案

    在电商、外卖、票务等系统中,订单超时未支付自动取消是一个常见的需求,这个功能乍一看很简单,甚至很多初学者会觉得:"不就是加个定时器么?" 但真到了实际工作中,细节的复杂程度往往会超乎预期,本文给大家介绍了Java实现订单超时自动取消的7种方案
    2024-12-12
  • Java单测void类型的方法详解

    Java单测void类型的方法详解

    这篇文章主要给大家介绍了Java中单测void类型的方法,文中给出了详细的示例代码,相信对大家的理解和学习具有一定的参考借鉴价值,需要的朋友可以跟着小编下面来一起学习学习吧。
    2017-01-01
  • Java对Excel表格的上传和下载处理方法

    Java对Excel表格的上传和下载处理方法

    这篇文章主要介绍了Java对Excel表格的上传和下载处理方法,需要的朋友可以参考下
    2017-08-08
  • Java中BigDecimal类的使用详解

    Java中BigDecimal类的使用详解

    这篇文章主要介绍了Java中BigDecimal类的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • vue+springboot读取git的markdown文件并展示功能

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

    Markdown-it 是一个用于解析和渲染 Markdown 标记语言的 JavaScript 库,使用 Markdown-it,你可以将 Markdown 文本解析为 HTML 输出,并且可以根据需要添加功能、扩展语法或修改解析行为,本文介绍vue+springboot读取git的markdown文件并展示,感兴趣的朋友一起看看吧
    2024-01-01
  • Spring Security 自定义授权服务器实践记录

    Spring Security 自定义授权服务器实践记录

    授权服务器(Authorization Server)目前并没有集成在Spring Security项目中,而是作为独立项目存在于Spring生态中,这篇文章主要介绍了Spring Security 自定义授权服务器实践,需要的朋友可以参考下
    2022-08-08
  • MyBatis传入List集合查询数据问题

    MyBatis传入List集合查询数据问题

    这篇文章主要介绍了MyBatis传入List集合查询数据问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02

最新评论