zoukankan      html  css  js  c++  java
  • java 发送微信客服消息

    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 += "}}";
    		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);
    	}
    
    	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);
    		}
    	}
    
    }

      

  • 相关阅读:
    一些我遇到前端方面的问题和解决方法
    Effective Objective-C 2.0学习记录(二)
    Effective Objective-C 2.0学习(一)
    加快Xcode运行速度
    JPA CriteriaBuilder的简单使用
    日志切分
    iOS并发,串行,异步,同步
    服务重启脚本
    简述http/https加密和认证方式
    nohup的使用
  • 原文地址:https://www.cnblogs.com/wggWeb/p/3767111.html
Copyright © 2011-2022 走看看