码迷,mamicode.com
首页 > 微信 > 详细

java 发送微信客服消息

时间:2014-06-06 10:59:55      阅读:549      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

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
package com.baosight.wechat.service;
 
import net.sf.json.JSONObject;
 
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
 
import com.baosight.wechat.project.baosightecmp.ConstantUtilEcmp;
import com.baosight.wechat.util.HttpUtil;
 
public class TestUnit
{
 
    @Test
    public void Test1()
    {
 
        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 += "}}";
 
        System.out.println(url);
        this.post(url, strJson);
    }
 
    public void post(String url, String json)
    {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        try
        {
            StringEntity s = new StringEntity(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)
        {
            throw new RuntimeException(e);
        }
    }
 
}<br>

  bubuko.com,布布扣

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

java 发送微信客服消息

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/wggWeb/p/3767111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!