HttpClient工具类

java 版本的 HttpClient工具类

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
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 HttpClientUtils {

 private static int SOCKET_TIME_OUT=1000*60*15;//15分钟
 private static int CONNECT_TIME_OUT=1000*60*15;//15分钟
 
 private static int CONNECT_REQUEST_TIME_OUT=1000*60*15;//15分钟

 /**
  * http的get请求
  * @param url
  */
 public static String get(String url) {
  return get(url, "UTF-8");
 }

 /**
  * http的get请求
  * @param url
  */
 public static String get(String url, String charset) {
  HttpGet httpGet = new HttpGet(url);
  RequestConfig config=RequestConfig.custom().setConnectTimeout(CONNECT_TIME_OUT).
    setSocketTimeout(SOCKET_TIME_OUT).
    setConnectionRequestTimeout(CONNECT_REQUEST_TIME_OUT).
    setExpectContinueEnabled(false).build();
  httpGet.setConfig(config);
  return executeRequest(httpGet, charset);
 }
/**
  * http的post请求,传递map格式参数
  */
 public static String post(String url, Map<String, String> dataMap) {
  return post(url, dataMap, "UTF-8");
 }
/**
  * http的post请求,传递map格式参数
  */
 public static String post(String url, Map<String, String> dataMap, String charset) {
  HttpPost httpPost = new HttpPost(url);
  try {
   if (dataMap != null){
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : dataMap.entrySet()) {
     nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset);
    formEntity.setContentEncoding(charset);
    httpPost.setEntity(formEntity);
   }
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return executeRequest(httpPost, charset);
 }
 /**
  * http的post请求,增加异步请求头参数,传递map格式参数
  */
 public static String ajaxPost(String url, Map<String, String> dataMap) {
  return ajaxPost(url, dataMap, "UTF-8");
 }

/**
  * http的post请求,增加异步请求头参数,传递map格式参数
  */
 public static String ajaxPost(String url, Map<String, String> dataMap, String charset) {
  HttpPost httpPost = new HttpPost(url);
  httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
  try {
   if (dataMap != null){
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : dataMap.entrySet()) {
     nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset);
    formEntity.setContentEncoding(charset);
    httpPost.setEntity(formEntity);
   }
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return executeRequest(httpPost, charset);
 }
/**
  * http的post请求,增加异步请求头参数,传递json格式参数
  */
 public static String ajaxPostJson(String url, String jsonString) {
  return ajaxPostJson(url, jsonString, "UTF-8");
 }

 /**
  * http的post请求,增加异步请求头参数,传递json格式参数
  */
 public static String ajaxPostJson(String url, String jsonString, String charset) {
  HttpPost httpPost = new HttpPost(url);
  httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
//  try {
   StringEntity stringEntity = new StringEntity(jsonString, charset);// 解决中文乱码问题
   stringEntity.setContentEncoding(charset);
   stringEntity.setContentType("application/json");
   httpPost.setEntity(stringEntity);
//  } catch (UnsupportedEncodingException e) {
//   e.printStackTrace();
//  }
  return executeRequest(httpPost, charset);
 }

/**
  * 执行一个http请求,传递HttpGet或HttpPost参数
  */
 public static String executeRequest(HttpUriRequest httpRequest) {
  return executeRequest(httpRequest, "UTF-8");
 }
/**
  * 执行一个http请求,传递HttpGet或HttpPost参数
  */
 public static String executeRequest(HttpUriRequest httpRequest, String charset) {
  CloseableHttpClient httpclient;
 
   httpclient = HttpClients.createDefault();
  String result = "";
  try {
   try {
    CloseableHttpResponse response = httpclient.execute(httpRequest);
    HttpEntity entity = null;
    try {
     entity = response.getEntity();
     result = EntityUtils.toString(entity, charset);
    } finally {
     EntityUtils.consume(entity);
     response.close();
    }
   } finally {
    httpclient.close();
   }
  }catch(IOException ex){
   ex.printStackTrace();
  }
  return result;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值