java httpclientutil_HttpClientUtil

package com..utils;

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.nio.charset.Charset;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

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.DefaultHttpClient;

import lombok.extern.slf4j.Slf4j;

/*

* httpclientUtil

*

*/

@Slf4j

public class HttpClientUtil {

/**

* post

*

* @param sendData

* @param url

* @return

* @throws Exception

*/

public static String doPost(String sendData, String url, Map header)throws Exception {

String result =null;

try {

HttpResponse response =postRes(sendData, url, header);

HttpEntity httpEntity = response.getEntity();

InputStream instreams = httpEntity.getContent();

result =convertStreamToString(instreams);

}catch (Exception ex) {

throw ex;

}

return result;

}

/**

* post response

*

* @param sendData

* @param url

* @return

* @throws Exception

*/

public static HttpResponse postRes(String sendData, String url, Map header)throws Exception {

HttpClient httpClient =null;

HttpPost httpPost =null;

httpClient =new DefaultHttpClient();

httpPost =new HttpPost(url);

//    httpPost.addHeader("Content-type", "application/json; charset=utf-8");

//    httpPost.setHeader("Accept", "application/json");

StringEntity entity =new StringEntity(sendData, Charset.forName("UTF-8"));

//    entity.setContentEncoding("UTF-8");

//    entity.setContentType("application/json");

if (header !=null && !header.isEmpty()) {

for (Map.Entry map : header.entrySet()) {

httpPost.addHeader(map.getKey(), map.getValue());

}

}

httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);

return response;

}

/**

* httpClient get

*

* @param url

* @return

* @throws Exception

*/

public static String doGet(String url, Map header)throws Exception {

String result =null;

try {

HttpResponse response =getRes(url, header);

HttpEntity httpEntity = response.getEntity();

if (null != httpEntity) {

InputStream instreams = httpEntity.getContent();

result =convertStreamToString(instreams);

}

}catch (Exception e) {

throw e;

}

return result;

}

/**

* httpClient get response

*

* @param url

* @return

* @throws Exception

*/

public static HttpResponse getRes(String url, Map header)throws Exception {

HttpClient httpClient =null;

HttpGet httpGet =null;

httpClient =new DefaultHttpClient();

httpGet =new HttpGet(url);

if (header !=null && !header.isEmpty()) {

for (Map.Entry map : header.entrySet()) {

httpGet.addHeader(map.getKey(), map.getValue());

}

}

System.out.println("get method url-------------------------->>>" + httpGet.getURI());

HttpResponse response = httpClient.execute(httpGet);

return response;

}

public static String convertStreamToString(InputStream is, HttpServletResponse response,boolean record,

String contenyType)throws IOException {

StringBuilder sb1 =new StringBuilder();

if (!record) {

sb1.append("此方法为:").append(contenyType).append(',');

}

ByteArrayOutputStream baos =new ByteArrayOutputStream();

try {

IOUtils.copy(is, baos);

if (record) {

String str =new String(baos.toByteArray(),"UTF-8");

sb1.append(str);

}else {

sb1.append("长度:").append(baos.toByteArray().length);

}

response.getOutputStream().write(baos.toByteArray());

response.getOutputStream().flush();

}catch (IOException e) {

throw e;

}

return sb1.toString();

}

public static String convertStreamToString(InputStream is)throws IOException {

StringBuilder sb1 =new StringBuilder();

byte[] bytes =new byte[4096];

int size =0;

try {

while ((size = is.read(bytes)) >0) {

String str =new String(bytes,0, size,"UTF-8");

sb1.append(str);

}

}catch (IOException e) {

throw e;

}finally {

try {

is.close();

}catch (IOException e) {

throw e;

}

}

return sb1.toString();

}

/**

*

*

* @param request

* @return

* @throws IOException

*/

public static String requestBody(HttpServletRequest request)throws IOException {

ByteArrayOutputStream bos =new ByteArrayOutputStream();

IOUtils.copy(request.getInputStream(),bos);

//    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

//    String line;

//    StringBuilder sb = new StringBuilder();

//    while ((line = br.readLine()) != null) {

//      sb.append(line).append('\n');

//    }

//    String rsb = sb.toString();

String rsb =new String(bos.toByteArray(),"utf-8");

if(rsb.length()>200) {

log.info("request body length {} b:{}",bos.size(), rsb.substring(0,199));

}else {

log.info("request body length {} b:{}",bos.size(), rsb);

}

return rsb;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值