package com.bootdo.common.utils;
import java.io.IOException;
import java.util.;
import com.bootdo.common.domain.HttpParamEntity;
import com.bootdo.common.domain.OsHttpDelete;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
public static String sendUrl(HttpParamEntity httpParamEntity,String type) {
Map <String,String>headParam = httpParamEntity.getHeadParam();
Set<Map.Entry<String, String>> set = headParam.entrySet();
Iterator<Map.Entry<String, String>> iterator = set.iterator();
Map.Entry<String, String> entry=null;
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
HttpEntityEnclosingRequestBase httpRquest=null;
if(type.equals("put")){
System.out.println("put 操作..............");
httpRquest = new HttpPut(httpParamEntity.getUrl());
}else if(type.equals("post")){
System.out.println("post 操作..............");
httpRquest = new HttpPost(httpParamEntity.getUrl());
}else if(type.equals("get")){
// httpRquest = new HttpGet(httpParamEntity.getUrl());
}else if (type.equals("patch")){
httpRquest = new HttpPatch(httpParamEntity.getUrl());
}else if(type.equals("delete")){
httpRquest = new OsHttpDelete(httpParamEntity.getUrl());
// httpDelete.set
}
while(iterator.hasNext()){
entry = iterator.next();
httpRquest.setHeader(entry.getKey(),entry.getValue());
}
try {
if(httpParamEntity.getBody()!=null){
httpRquest.setEntity(new StringEntity(httpParamEntity.getBody(),"utf-8"));
}
httpResponse = httpClient.execute(httpRquest);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
return result;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/* public static void main(String[] args) {
HttpParamEntity httpParamEntity=new HttpParamEntity();
httpParamEntity.getHeadParam().put(“cpid”,“167”);
httpParamEntity.getHeadParam().put(“key”,“6f5a3292c3c4731aec91df0a0581c278”);
httpParamEntity.getHeadParam().put(“Content-Type”,“application/json”);
httpParamEntity.setUrl(“http://api.jiai.pro:8080/jiai/familyNumber/3”);
httpParamEntity.getHeadParam().put(“imei”,“356191943756372”);
DeviceDO deviceDO = new DeviceDO();
deviceDO.setImei(“356191943756372”);
httpParamEntity.setBody(JSON.toJSONString(deviceDO));
String result = HttpClientUtil.sendUrl(httpParamEntity, “delete”);
System.out.println(“result:”+result);
}*/
}