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;
}
}