|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
packagecom.baosight.wechat.service;
importnet.sf.json.JSONObject;
importorg.apache.commons.httpclient.HttpStatus;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.entity.StringEntity;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
importorg.junit.Test;
importcom.baosight.wechat.project.baosightecmp.ConstantUtilEcmp;
importcom.baosight.wechat.util.HttpUtil;
publicclassTestUnit
{
@Test
publicvoidTest1()
{
String Access_token = HttpUtil.getAccess_token_server(ConstantUtilEcmp.APPID, ConstantUtilEcmp.APPSECRET);
// String open_id = "oe7rSjlz1flhx7HP3-DnlgrpobqM";
JSONObject obj = JSONObject.fromObject(Access_token);
String token = obj.getString("access_token");
String strJson = "{\"touser\" :\"oe7rSjlz1flhx7HP3-DnlgrpobqM\",";
strJson += "\"msgtype\":\"text\",";
strJson += "\"text\":{";
strJson += "\"content\":\"Hello World\"";
strJson += "}}";
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?&body=0&access_token="+ token;
System.out.println(url);
this.post(url, strJson);
}
publicvoidpost(String url, String json)
{
DefaultHttpClient client = newDefaultHttpClient();
HttpPost post = newHttpPost(url);
try
{
StringEntity s = newStringEntity(json);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = res.getEntity();
System.out.println(EntityUtils.toString(entity, "utf-8"));
}
}
catch(Exception e)
{
thrownewRuntimeException(e);
}
}
}<br>
|

java 发送微信客服消息,布布扣,bubuko.com