package com.tf.jcfx.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import com.google.gson.Gson;
import com.tf.jcfx.vo.util.JsonRootBean;
public class HttpUtils {
private static final Log log = LogFactory.getLog(HttpUtils.class);
/**
* post请求
*
* @param url
* @param param
* @return
* @throws Exception
*/
public static String httpPost(String url, Map<String, Object> map) throws Exception {
String result = null;
try {
HttpPost post = new HttpPost(url);
//requestConfig post请求配置类,设置超时时间
RequestConfig requestConfig = RequestConfig.custom().build();
post.setConfig(requestConfig);
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != null && entry.getValue() != "") {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue() + ""));
}
}
//在这个地方设置编码 防止请求乱码
post.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse httpResponse = HttpClients.createDefault().execute(post);
System.out.println("返回数据:" + httpResponse);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出应答字符串
} catch (Exception e) {
throw e;
}
return result;
}
public static String doPost(String url, String json) throws Exception {
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(json, ContentType.APPLICATION_JSON, "UTF-8", false); // 中文乱码在此解决
post.setEntity(s);
CloseableHttpResponse res = HttpClients.createDefault().execute(post);
if (res.getCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
return result;
}
} catch (Exception e) {
throw e;
}
return null;
}
public static String SendMessage(String userCode,String message,String title,String description,String module,String moduleParam) {
String httpPost="";
try {
Map<String, Object> map=new HashMap<String, Object>();
map.put("systemToken",getMesssageToken());
map.put("messageCode",AssitUtil.getMemoryPiece("messageCode").toString());
map.put("userCode",userCode);
map.put("title",title);
map.put("description",description);
map.put("content",message);
map.put("module",module);
map.put("moduleParam",moduleParam);
httpPost = HttpUtils.doPost(AssitUtil.getMemoryPiece("sendMessageUrl").toString(), new Gson().toJson(map));
}catch(Exception e){
log.error("推送失败!");
e.printStackTrace();
}
return httpPost;
}
public static String getMesssageToken() throws Exception {
String doPost = HttpUtils.doPost(AssitUtil.getMemoryPiece("messsageTokenUrl").toString(), "");
Gson gson = new Gson();
JsonRootBean bean = gson.fromJson(doPost,JsonRootBean.class);
return bean.getData().getToken();
}
public static void main(String[] args) throws Exception {
/* Map<String, Object> map=new HashMap();
map.put("systemToken","8ee937427e344bed80639751edf9832d");
map.put("messageCode","02ae61c9883c4cfdb5fba1122179c0b6");
map.put("userCode","17461");
map.put("title","文件");
map.put("description","文件");
map.put("content","文件");
map.put("module","sto.com.shtb.native.mobile.datacenter");
map.put("moduleParam","打的");
String json =new Gson().toJson(map);
String httpPost = HttpUtils.doPost("http://10.10.14.146/api/message-center/pc/message_center/v2/push", json);*/
/* String insertMessage = SendMessage("17461","该吃饭了");
if (!insertMessage.contains("\"result\":\"0\"")) {
log.warn(" " + insertMessage);
}
System.out.println(insertMessage);*/
/* String doPost = HttpUtils.doPost("http://10.10.14.146/api/auth-server/pc/com/token?comId=02ab648587494f62bf563fd20a607e7d", "");
System.out.println(doPost);
Gson gson = new Gson();
JsonRootBean bean = gson.fromJson(doPost,JsonRootBean.class);
System.out.println(bean.getData().getToken());*/
}
}
JsonRootBean 对象
package com.tf.jcfx.vo.util;
/**
* Copyright 2022 json.cn
*/
/**
* Auto-generated: 2022-10-30 10:15:18
*
* @author json.cn ([email protected])
* @website http://www.json.cn/java2pojo/
*/
public class JsonRootBean {
private String result;
private String message;
private Data data;
private String totalCount;
private String expMessage;
public void setResult(String result) {
this.result = result;
}
public String getResult() {
return result;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setData(Data data) {
this.data = data;
}
public Data getData() {
return data;
}
public void setTotalCount(String totalCount) {
this.totalCount = totalCount;
}
public String getTotalCount() {
return totalCount;
}
public void setExpMessage(String expMessage) {
this.expMessage = expMessage;
}
public String getExpMessage() {
return expMessage;
}
}
标签:map,封装,String,HttpUtils,JsonRootBean,http,put,import,public From: https://www.cnblogs.com/zhil/p/17028647.html