Commit d31b98d5 by hzc

集成企业微信消息通知

parent 355307d8
package com.keymobile.indicators.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
public static final String CHARSET = "UTF-8";
// 发送get请求 url?a=x&b=xx形式
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlName = "";
if (param.length() != 0) {
urlName = url + "?" + param;
} else
urlName = url;
URL resUrl = new URL(urlName);
URLConnection urlConnec = resUrl.openConnection();
urlConnec.setRequestProperty("accept", "*/*");
urlConnec.setRequestProperty("connection", "Keep-Alive");
urlConnec.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
urlConnec.connect();
Map<String, List<String>> map = urlConnec.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(urlConnec.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送get请求失败" + e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
// 发送post请求
public static String sendPost(String url, MultipartHttpServletRequest param) {
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
URL resUrl = new URL(url);
URLConnection urlConnec = resUrl.openConnection();
urlConnec.setRequestProperty("accept", "*/*");
urlConnec.setRequestProperty("connection", "Keep-Alive");
urlConnec.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
urlConnec.setDoInput(true);
urlConnec.setDoOutput(true);
out = new PrintWriter(urlConnec.getOutputStream());
out.print(param);// 发送post参数
out.flush();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(urlConnec.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("post请求发送失败" + e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
//post请求方法
public static String sendPost(String url, Map<String,Object> params) {
String response = null;
// System.out.println(url);
// System.out.println(params);
try {
List<NameValuePair> pairs = null;
if (params != null && !params.isEmpty()) {
pairs = new ArrayList<NameValuePair>(params.size());
for (String key : params.keySet()) {
pairs.add(new BasicNameValuePair(key, params.get(key).toString()));
}
}
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
// StringEntity stringentity = new StringEntity(data);
if (pairs != null && pairs.size() > 0) {
httppost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
}
httpresponse = httpclient.execute(httppost);
response = EntityUtils
.toString(httpresponse.getEntity(),"UTF-8");
// System.out.println(response);
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public static String jsonPost(String url, String param){
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String response = null;
try {
StringEntity s = new StringEntity(param,CHARSET);
s.setContentEncoding(CHARSET);
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = httpclient.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
response = EntityUtils.toString(res.getEntity(),CHARSET);// 返回json格式:
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("dimDesc","asda萨达撒打算撒打算");
jsonObject.put("id",45);
// sendPost("http://192.168.1.56:8080/smkj/api/lcds/user/lcdsUser",map);
String s = jsonPost("http://localhost:9088/indDim/test", jsonObject.toJSONString());
System.out.println(s);
}
/**
* 测试
* 说明:这里用新浪股票接口做get测试,新浪股票接口不支持jsonp,至于post,因为本人用的公司的接口就不展示了,一样的,一个url,一个数据包
*/
/*
* public static void main(String[] args) { // TODO Auto-generated method
* stub String resultGet = sendGet("http://hq.sinajs.cn/list=sh600389","");
* System.out.println(resultGet); }
*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment