httputils

HTTPutils

package com.server.publish.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

public class HttpUtils {
	public static String get(String url, Map<String, String> headMap) throws Exception {
		HttpGet httpGet = new HttpGet(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpGet.addHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static InputStream down(String url, Map<String, String> headMap) throws Exception {
		url = url.replaceAll("\\\\", "/");
		HttpGet httpGet = new HttpGet(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpGet.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		InputStream input = entity.getContent();
		return input;
	}

	public static String post(String url, Map<String, String> headMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static String post(String url, List<NameValuePair> params, Map<String, String> headMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		// 添加参数
		httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static String post(String url, String entityString, Map<String, String> headMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		StringEntity stringEntity = new StringEntity(entityString, Charset.forName("UTF-8"));
		stringEntity.setContentType("application/json");
//		stringEntity.setContentEncoding("utf-8");
		httpPost.setEntity(stringEntity);
		httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static String delete(String url, Map<String, String> headMap) throws Exception {
		HttpDelete httpDelete = new HttpDelete(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpDelete.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpDelete);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	/**
	 * 将流 保存为数据数组
	 *
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		// 创建一个Buffer字符串
		byte[] buffer = new byte[1024];
		// 每次读取的字符串长度,如果为-1,代表全部读取完毕
		int len = 0;
		// 使用一个输入流从buffer里把数据读取出来
		while ((len = inStream.read(buffer)) != -1) {
			// 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
			outStream.write(buffer, 0, len);
		}
		// 关闭输入流
		inStream.close();
		// 把outStream里的数据写入内存
		return outStream.toByteArray();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值