微信企业号

微信企业号

public class Helloworld {
	private static String corpId="wx9e6b5f7e6b9f86a2";
	private static String corpsecret="vsKgDKA-m4wDlZCRfkv33FgJIuIz07xnX8Cv43pykKZ-D1BYcUiou0Pn7hH6bRmi";
	public static void main(String[] args) {
		Helloworld helloWorld =new Helloworld();
		
		String jsonContext="{" +
				"\"touser\":\"hrcui\"," +
				"\"toparty\":\"\"," +
				"\"msgtype\":\"text\"," +
				"\"agentid\":4," +
				"\"text\":{" +
				"\"content\":\"lckj,helloworld!!!\"}," +
				"\"safe\":0}";
		helloWorld.sendReqMsg(jsonContext, corpId, corpsecret);
		
//		String createMenuStr="{ \"button\":[ {\"type\":\"click\",\"name\":\"菜单功能\",\"key\":\"TION\"}," +
//				"{\"name\":\"一级菜单\",\"sub_button\":[{\"type\":\"view\",\"name\":\"博客\",\"url\":\"http://www.baidu.com\"}," +
//				"{\"type\":\"click\",\"name\":\"二级菜单最多多少个字\",\"key\":\"KEY1\"}]}]}";
//		helloWorld.createMenu(createMenuStr, 4+"");
	}
	
	
	/**
	 * 发消息
	 * @param jsonContext json字符串
	 * @param corpId 微信企业号标识
	 * @param corpsecret 管理组凭证秘钥
	 * @return
	 * */
	public JSONObject sendReqMsg(String jsonContext,String corpId,String corpsecret){
		JSONObject result=null;
//		String token=getTokenFromWx(corpId,corpsecret);
		//带缓存获取accessToken
		String token=AccessTokenCache.getTokenFromWx(corpId, corpsecret);
		System.out.println(token);
		try{
			CloseableHttpClient httpclient=HttpClients.createDefault();
			HttpPost httpPost=new HttpPost("https://qyapi.weixin.qq.com"+"/cgi-bin/message/send?access_token="+token);
			StringEntity myEntity=new StringEntity(jsonContext,ContentType.create("text/plain", "UTF-8"));
			httpPost.setEntity(myEntity);
			ResponseHandler<JSONObject> responseHandler=new ResponseHandler<JSONObject>() {
				public JSONObject handleResponse(final HttpResponse response)throws ClientProtocolException,IOException{
					int status =response.getStatusLine().getStatusCode();
					if(status>=200&&status<300){
						HttpEntity entity=response.getEntity();
						if(null!=entity){
							String result=EntityUtils.toString(entity);
							JSONObject resultObj=JSONObject.fromObject(result);
							return resultObj;
						}else{
							return null;
						}
					}else{
						throw new ClientProtocolException("Unexpected response status: "+status);
					}
				}
			};
			JSONObject responseBody=httpclient.execute(httpPost,responseHandler);
			System.out.println(responseBody.toString());
			result=responseBody;
			httpclient.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
	public boolean createMenu(String menuStr,String agentId){
		boolean flag=false;
		String token=AccessTokenCache.getTokenFromWx(corpId, corpsecret);
		try{
			CloseableHttpClient httpclient=HttpClients.createDefault();
			HttpPost httpPost=new HttpPost("https://qyapi.weixin.qq.com"+"/cgi-bin/menu/create?access_token="+token+"&agentId="+agentId);
			StringEntity myEntity=new StringEntity(menuStr,ContentType.create("text/plain", "UTF-8"));
			httpPost.setEntity(myEntity);
			ResponseHandler<JSONObject> responseHandler=new ResponseHandler<JSONObject>() {
				public JSONObject handleResponse(final HttpResponse response)throws ClientProtocolException,IOException{
					int status =response.getStatusLine().getStatusCode();
					if(status>=200&&status<300){
						HttpEntity entity=response.getEntity();
						if(null!=entity){
							String result=EntityUtils.toString(entity);
							JSONObject resultObj=JSONObject.fromObject(result);
							return resultObj;
						}else{
							return null;
						}
					}else{
						throw new ClientProtocolException("Unexpected response status: "+status);
					}
				}
			};
			JSONObject responseBody=httpclient.execute(httpPost,responseHandler);
			System.out.println(responseBody.toString());
			httpclient.close();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值