HTTPClient封装Util

HTTPClient封装Util

copy即用

package com.qin.bill.utils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
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.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

	/**
	 * 
	 * Get请求
	 * 
	 * @param url
	 * 
	 * @param params
	 * 
	 * @return
	 */
	public static String getForInter(String url) {
		return getForInter(url, null, null);
	}

	/**
	 * 
	 * Get请求
	 * 
	 * @param url
	 * 
	 * @param params
	 * 
	 * @return
	 */
	public static String getForInter(String url, List<NameValuePair> params) {
		return getForInter(url, params, null);
	}

	/**
	 * 
	 * Get请求
	 * 
	 * @param url
	 * 
	 * @param params
	 * 
	 * @return
	 */
	public static String getForInter(String url, List<NameValuePair> params,
			List<NameValuePair> headerParams) {
		CloseableHttpClient hc = HttpClients.createDefault();
		String body = null;
		try {
			// Get请求
			HttpGet httpget = new HttpGet(url);
			// 设置参数
			if (params != null && params.size() > 0) {
				String str = EntityUtils.toString(new UrlEncodedFormEntity(
						params));
				httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
			}
			if (headerParams != null) {
				for (NameValuePair param : headerParams) {
					if (param != null && param.getName() != null
							&& param.getValue() != null) {
						httpget.setHeader(param.getName(), param.getValue());
					}
				}
			}
			// 发送请求
			HttpResponse httpresponse = hc.execute(httpget);
			// 获取返回数据
			HttpEntity entity = httpresponse.getEntity();
			// body = EntityUtils.toString(entity, "UTF-8");
			body = new String(EntityUtils.toByteArray(entity), "UTF-8");
			EntityUtils.consume(entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				hc.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return body;
	}

	/**
	 * 
	 * Post请求
	 * 
	 * @param url
	 * 
	 * @param params
	 * 
	 * @return
	 */

	public static String post(String url) {
		return post(url, null, null);
	}
	
	public static String post(String url, String sendBody) {
		return post(url, sendBody, null);
	}

	public static String post(String url, String sendBody,
			Map<String, String> headerParams) {
		if (StringUtils.isBlank(sendBody)) {
			sendBody = "{}";
		}
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String body = null;
		try {
			// Post请求
			HttpPost post = new HttpPost(url);
			StringEntity se = new StringEntity(sendBody, "UTF-8");
			se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8"));
			post.setEntity(se);
			if (headerParams != null) {
				for (Map.Entry<String, String> entry : headerParams.entrySet()) {
					post.setHeader(entry.getKey(), entry.getValue());
				}
			}
			// 发送请求
			HttpResponse httpresponse = httpClient.execute(post);
			// 获取返回数据
			HttpEntity entity = httpresponse.getEntity();

			body = new String(EntityUtils.toByteArray(entity), "UTF-8");
			EntityUtils.consume(entity);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return body;
	}

	public static String putForInter(String url, String sendBody) {
		return putForInter(url, sendBody, null);
	}

	public static String putForInter(String url, String sendBody,
			List<NameValuePair> headerParams) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String body = null;
		try {
			// Put请求
			HttpPut httpput = new HttpPut(url);
			StringEntity se = new StringEntity(sendBody, "UTF-8");
			se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8"));
			httpput.setEntity(se);
			if (headerParams != null) {
				for (NameValuePair param : headerParams) {
					if (param != null && param.getName() != null
							&& param.getValue() != null) {
						httpput.setHeader(param.getName(), param.getValue());
					}
				}
			}
			// 发送请求
			HttpResponse httpresponse = httpClient.execute(httpput);
			// 获取返回数据
			HttpEntity entity = httpresponse.getEntity();
			body = new String(EntityUtils.toByteArray(entity), "UTF-8");
			EntityUtils.consume(entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return body;
	}
	
	public static String postFile(String url,String filePath) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String body = null;
		try {
			// Post请求
			HttpPost post = new HttpPost(url);
			FileEntity reqEntity = new FileEntity(new File(filePath), ContentType.MULTIPART_FORM_DATA);
            // 设置请求编码
            reqEntity.setContentEncoding("UTF-8");
            post.setEntity(reqEntity); 
			// 发送请求
			HttpResponse httpresponse = httpClient.execute(post);
			// 获取返回数据
			HttpEntity entity = httpresponse.getEntity();
			body = new String(EntityUtils.toByteArray(entity), "UTF-8");
			EntityUtils.consume(entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return body;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值