发送消息给单一用户

/**
	 * 发送客服消息给单一用户
	 * @param openId      用户唯一标示
	 * @param message     发送给用户的消息
	 */
	public void senMessageToSingleUser(String openId, String message, String accessToken) {
		String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken;
		try {
			WXMessage wxMessage = new WXMessage();
			wxMessage.setMsgtype("text");
			wxMessage.setTouser(openId);
			WXMessageText text = new WXMessageText();
			text.setContent(message);
			wxMessage.setText(text);
			Gson gson = new Gson();
			String msg = gson.toJson(wxMessage);
			URL url = new URL(action);
			HttpURLConnection http = (HttpURLConnection) url.openConnection();
			http.setRequestMethod("POST");
			http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			http.setDoOutput(true);
			http.setDoInput(true);
			System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
			System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒

			http.connect();
			OutputStream os = http.getOutputStream();
			os.write(msg.getBytes("UTF-8"));// 传入参数
			os.flush();
			os.close();

			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message1 = new String(jsonBytes, "UTF-8");
			System.out.println(message1);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



public static String sendPost(String url, String content, String accessToken) throws IOException {
		String action = url + "?access_token=" + accessToken;

		URL _url = new URL(action);
		HttpURLConnection http = (HttpURLConnection) _url.openConnection();
		http.setRequestMethod("POST");
		http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		http.setDoOutput(true);
		http.setDoInput(true);
		System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
		System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒

		http.connect();
		OutputStream os = http.getOutputStream();
		os.write(content.getBytes("UTF-8"));// 传入参数
		os.flush();
		os.close();

		InputStream is = http.getInputStream();
		int size = is.available();
		byte[] jsonBytes = new byte[size];
		is.read(jsonBytes);
		String message1 = new String(jsonBytes, "UTF-8");
		return message1;

	}

/**
 * 微信客服消息实体类
 * @author Draven
 * @date 2016年9月28日
 * @e-mail draven1122@163.com
 */
public class WXMessage {
	private String touser;
	private String msgtype;
	private WXMessageText text;
	public String getTouser() {
		return touser;
	}
	public void setTouser(String touser) {
		this.touser = touser;
	}
	public String getMsgtype() {
		return msgtype;
	}
	public void setMsgtype(String msgtype) {
		this.msgtype = msgtype;
	}
	public WXMessageText getText() {
		return text;
	}
	public void setText(WXMessageText text) {
		this.text = text;
	}
	
}


这里需要gson.jar进行对象与json的转换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值