博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
发送http请求
阅读量:6836 次
发布时间:2019-06-26

本文共 3515 字,大约阅读时间需要 11 分钟。

 

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

你可能感兴趣的文章
yuv和yCbCr的差异
查看>>
引擎设置
查看>>
策略模式
查看>>
log
查看>>
深入浅出 JQuery (一) 浅析JQuery
查看>>
[暴力]JZOJ 5882 雪人
查看>>
对python选修课的感想
查看>>
解决select下拉框禁用(设置disabled属性),后台获取值为空
查看>>
第四周进度条
查看>>
http delete 方法传参数遇到java.net.ProtocolException: DELETE does not support writing的问题...
查看>>
列联表(频数表)
查看>>
root@mysqlproxy-Compaq:~# mysql -uhpproxy -p1234 -P4040 -h 192.168.19.110
查看>>
BZOJ 1061: [Noi2008]志愿者招募【单纯形裸题】
查看>>
【干货分享】dos命令大全
查看>>
Android:onActivityResult详解
查看>>
Can't drawInRect
查看>>
IOS开发之──应用之间调用
查看>>
Python中级 —— 07标准库
查看>>
Robot FrameWork基础学习(四) 元素定位
查看>>
jchdl - GSL实例 - Register
查看>>