package cn.newtol.weiXin.util;
import java.io.*;
import java.net.*;
import java.util.Map;
/**
* 用于curl调用接口,传递参数与获取结果采用输入输出流
*/
public class CurlUtil {
private static final int DEF_CONN_TIMEOUT = 30000;
private static final int DEF_READ_TIMEOUT = 30000;
private static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
/**
*
* @param strUrl 请求地址
* @param params 请求参数
* @param method 请求方法
* @return
*/
public static String getContent(String strUrl, Map<String, Object> params, String method) {
BufferedReader reader = null;
BufferedWriter writer = null;
HttpURLConnection conn = null;
String paramStr = null;
String result = null;
//有请求参数的话拼接成字符串
if (params != null && !params.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (Map.Entry i : params.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
paramStr = sb.toString();
}
if (StringUtil.isBlank(method) || method.equalsIgnoreCase("GET")) {
strUrl = strUrl + "?" + paramStr;
}
}
try {
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setRequestProperty("User-agent", userAgent);
conn.setInstanceFollowRedirects(false);
conn.connect();
if (!StringUtil.isBlank(method) && method.equalsIgnoreCase("POST")) {
conn.setRequestMethod("POST");
if (!StringUtil.isBlank(paramStr)) {
conn.setDoOutput(true);
writer = new BufferedWriter(
new OutputStreamWriter(
conn.getOutputStream(), "UTF-8"
)
);
writer.write(paramStr);
writer.flush();
}
}
reader = new BufferedReader(
new InputStreamReader(
conn.getInputStream(), "UTF-8"
)
);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
if (writer != null) writer.close();
if (conn != null) conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String postData(String strUrl, String data) {
String result = null;
try {
URL url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
connection.getOutputStream(), "UTF-8"
)
);
writer.write(data);
writer.flush();
BufferedReader reader = new BufferedReader(
new InputStreamReader(
connection.getInputStream(), "UTF-8"
)
);
String line = null;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
writer.close();
connection.disconnect();
result = builder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA实现微信公众号接收普通和被动回复普通消息
共111个文件
class:20个
xml:16个
sample:11个
4星 · 超过85%的资源 需积分: 48 37 下载量 66 浏览量
2018-04-29
18:31:20
上传
评论
收藏 684KB ZIP 举报
温馨提示
JAVA实现了微信公众号的接收普通和被动回复消息的类型
资源推荐
资源详情
资源评论
收起资源包目录
JAVA实现微信公众号接收普通和被动回复普通消息 (111个子文件)
0130ed2800fb67cf0bc17a4cc03ba45117cdde 748B
0c8d95f8af6dae956ab00966021cb9d1ba440c 1KB
140d08afc28d98119a718096a6d7fffa1fc86f 110B
143a03b91ace7d4c78d9c4d842823341868fdb 165B
1f7fee9a593458e51e324715b79e155bff914a 1KB
25d38bd782a2c5f1828f26c907df6d83253307 81B
30203852d89b9b48829942e6c03166a84c1e79 46B
37f229a3bea1e860bead2fbf8fb99a5da73099 172B
39f2e4a6161d7fa051cb833ddb65bcb753ff48 49B
4398f42f4d48f9a7d1fba184f846e90f2d15a7 261B
49aaac142801beff924e52caa86ca350593086 815B
4e7460d073771db8b3be7cb9737c097c5784eb 189B
52a4bccb01ae83215e50c1b56180fdb1857a59 44B
5af3afa415352e73a098d1f9a0b772e1dba677 48B
5b6e0498af62cc02e52630c227d816ede11869 52B
5eb14f32c691846aed49156c1dcff43b02e20f 1KB
65829e71747a257354ce53ca532077e0078da4 802B
6ae6b2b3af2665413f8f8d21cc12b0095ec6a0 76B
796c0e66c437c4a69dcd14a75403b9246ddd9d 105B
796d4002230f82b0fbbad62a28c33385c04ab7 254B
7a78c0b866d671dc225e976d9b5dfa985f5f42 78B
8169bb958579c635a5c09ee2f379cc5956c0c2 56B
84bb65a6c264f7194be92841a0dfba45e435af 62B
85b340e28324b4a0e0de123b961fb831efa9a6 806B
8fb178996b29e18e212679904b53c8db4ab1f9 85B
96658cc0c1549c9c57f99a904c059228b9333e 52B
ab3c2fae34c45e3fb35d0f91780c84aa413eca 1KB
b1770ec1658832d43de36abe3a996a46d6c6b8 188B
b25e95ae8e0dd2808aa4cad6067cefc78fd3c6 48B
CurlUtil.class 5KB
CurlUtil.class 5KB
IndexServlet.class 5KB
IndexServlet.class 5KB
Menu.class 4KB
Menu.class 4KB
Button.class 4KB
Button.class 4KB
MessageServiceImp.class 3KB
MessageServiceImp.class 3KB
AccessToken.class 3KB
AccessToken.class 3KB
EncryptUtil.class 2KB
EncryptUtil.class 2KB
StringUtil.class 766B
StringUtil.class 766B
Const.class 523B
Const.class 523B
MessageService.class 477B
MessageService.class 477B
COMMIT_EDITMSG 25B
config 239B
ddbdfd09da2dfa4bf14a39314ed0499aeff41e 923B
description 73B
e02ba2966a12f6553f83d98fa214cb85bd1301 46B
e2f3fa7a5d0bc0ce76dd1a56ffde842a1c8149 77B
e421b8ee97bc733eb320663f23ef7545ce1951 48B
e4f5f110a478b59566ef1e32cca1e673f849c1 45B
eb1ddfbbc029bcab630581847471d7f238ec53 140B
exclude 240B
f04a2794a424c2454e89cdf9fbd4db8135fc19 319B
fe8bcf4f5443b4fc3bca8fae8eb2c825702aae 88B
HEAD 348B
HEAD 23B
weiXin.iml 1KB
index 2KB
jedis-2.9.0.jar 541KB
commons-pool2-2.4.2.jar 109KB
CurlUtil.java 5KB
IndexServlet.java 4KB
Button.java 4KB
Menu.java 4KB
AccessToken.java 2KB
EncryptUtil.java 2KB
MessageServiceImp.java 2KB
StringUtil.java 619B
Const.java 413B
MessageService.java 325B
index.jsp 52B
index.jsp 52B
master 348B
master 284B
master 41B
master 41B
MANIFEST.MF 90B
pre-rebase.sample 5KB
update.sample 4KB
fsmonitor-watchman.sample 3KB
pre-commit.sample 2KB
prepare-commit-msg.sample 1KB
pre-push.sample 1KB
commit-msg.sample 896B
pre-receive.sample 544B
applypatch-msg.sample 478B
pre-applypatch.sample 424B
post-update.sample 189B
workspace.xml 66KB
uiDesigner.xml 9KB
pom.xml 2KB
weiXin_war_exploded.xml 899B
compiler.xml 624B
共 111 条
- 1
- 2
资源评论
- graysonLee2019-01-07还可以吧,得多研究。
Newtol
- 粉丝: 503
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功