微信发消息

微信发消息

public class WxNoticeSend {
	/**
	 * 发送提醒
	 * @param jsonContext
	 * @return
	 */
	public boolean sendNotice(String jsonContext){
		JSONObject jsono = sendReqMsg(jsonContext, WxHelper.CORPID, WxHelper.RESP_MSG_SECRET);
		if("0".equals(jsono.get("errcode")))return true;
		else{
			//生成日志
		}
		return false;
	}
	
	/**
	 * 发消息
	 * @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);
		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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值