发送http请求

1.RequestDTO类

public class RequestDTO {
private MetainfoDTO metainfo;
private DataInfoDTO datainfo;
public RespSchemeDTO(){

}
public MetainfoDTO getMetainfo() {
return metainfo;
}
public void setMetainfo(MetainfoDTO metainfo) {
this.metainfo = metainfo;
}
public DataInfoDTO  getDatainfo() {
return datainfo;
}
public void setDatainfo(DataInfoDTO datainfo) {
this.datainfo = datainfo;
}

2、HttpClientUtil工具类

public class HttpClientUtil{
public String Static doPost(String url,String json){
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 3000);
HttpPost post = new HttpPost(url);
post.addHeader("Content-Type","application/json;charset=utf-8");

try{
StringEntity s = new StringEntity(json,"UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);

byte[] re;
String result = null;

if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
InputStream input = entity.getContent();
if (entity.isStreaming()){
re = org.apache.http.util.EntityUtils.toByteArray(entity);
result = new String(re,"iso-8859-1");
}else{
result = org.apache.http.util.EntityUtils.toString(entity);
}
return result;
}else
return null;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public Static<T> T JSONToObj(String json,Class<T> clazz){
T t=null;
try{
ObjectMapper objectMapper = new ObjectMapper();
t=objectMapper.readValue(json, clazz);
}catch(Exception e){
e.printStackTrace();
}
return t;
}
}

3、发送http请求

public class SendHttp{

String url="http://ip:port/run/app?trancode=app.services.business.push";

RequestDTO requestDTO=new RequestDTO();

MetainfoDTO metainfoDTO=new MetainfoDTO();
metainfoDTO.setField1(“field1”);

....

DataInfoDTO dataInfoDTO=new DataInfoDTO();
dataInfoDTO.setField2("field2");

....

requestDTO.setDatainfo(schemedto);
requestDTO.setMetainfo(resplist);

ObjectMapper objectMapper = new ObjectMapper();
String encryptString="";//加密前的json
String json="";//加密后的json
String decryptString="";//加密的结果
String result="";//解密的结果

try{
encryptString=objectMapper.writeValueAsString(requestDTO);//Method that can be used to serialize any Java value as a String
json=DES.encryptDES(encryptString);//将json字符串加密
}catch(IOException e){
e.printStackTrace();
}

decryptString=HttpClientUtil.doPost(url,json);
result=DES.decryptDES(decryptString);//解密
Map maps = (Map) HttpClientUtil.JSONToObj(result, Map.class);
....

}

4、HttpClientUtil类

public class HttpClientUtil {
public String sendHttpPost(String url,String json){
RequestConfig config = RequestConfig.custom()
.setSocketTimeout(150000)
.setConnectTimeout(150000)
.setConnectionRequestTimeout(150000)
.build();
CloseableHttpClient httpClient = null;
String responseContent = null;

HttpPost httpPost=new HttpPost(url);//创建HttpPost
httpPost.addHeader("Content-type","application/json;charset=utf-8");
httpPost.setHeader("Accept","application/json");
try{
StringEntity stringEntity = new StringEntity(json, "UTF-8");
stringEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(stringEntity);
httpPost.setConfig(config);
CloseableHttpResponse response=httpClient.execute(httpPost);
HttpEntity entity=response.getEntity();
String responseContent=EntityUtils.toString(entity,"UTF-8");
}catch(Exception e){
e.printStackTrace();
}
return responseContent;
}
}

原文地址:https://www.cnblogs.com/BonnieWss/p/8916993.html