成都创新互联网站制作重庆分公司

外部项目接口工具类怎么利用HttpClient进行调用

今天就跟大家聊聊有关外部项目接口工具类怎么利用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 maps) { 
    HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost  
    // 创建参数队列  
    List nameValuePairs = new ArrayList(); 
    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进行调用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


本文题目:外部项目接口工具类怎么利用HttpClient进行调用
文章网址:http://cxhlcq.com/article/ieseeo.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部