欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 16:49:16
实例如下: import java.io.ioexception; import java.net.url; import java.util.arrayli...

实例如下:

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实现调用外部项目接口工具类的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。