微信开发相关的功能

/*

* 获取二维码的ticket

*/

public static String getTicket(){
        PrintWriter out;
        String parms = "{\"expire_seconds\": 1800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 1901}}}";
        String result = "";        
        String ticket = "";
        String info = http.sendPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+ ACCESS_TOKEN, parms);    
        JSONObject json = JSONObject.fromObject(info);
        if (json.has("ticket")) {
            ticket = json.getString("ticket");
        }
        return ticket;
    }
    
    /**
     * 获取二维码图片的URL
     * @param ticket
     * @return
     */
    public static String getEWM(String ticket){
        PrintWriter out;
        String info="";
        String url= "";        
        try {
            url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + URLEncoder.encode(ticket,"utf-8");
            
        } catch (Exception e) {
                e.printStackTrace();
        }
        return url;
    }


/**

     * 创建模板
     * @param args
     */
    public static void createTemplate(String params,String accessToken) {
        StringBuffer bufferRes = new StringBuffer();
        /*try {                
                URL realUrl = new URL("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ accessToken);
                 HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
                // 连接超时
                conn.setConnectTimeout(25000);
                // 读取超时 --服务器响应比较慢,增大时间
                conn.setReadTimeout(25000);
                HttpURLConnection.setFollowRedirects(true);
                // 请求方式
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
                conn.setRequestProperty("Referer", "https://api.weixin.qq.com/");
                conn.connect();
                
                // 获取URLConnection对象对应的输出流
                OutputStreamWriter outsw = new OutputStreamWriter(conn.getOutputStream());
                // 发送请求参数
                //out.write(URLEncoder.encode(params,"UTF-8"));
                outsw.write(params);
                outsw.flush();
                outsw.close();
                InputStream in = conn.getInputStream();
                BufferedReader read = new BufferedReader(new InputStreamReader(in,"UTF-8"));
                String valueString = null;
                while ((valueString=read.readLine())!=null){
                        bufferRes.append(valueString);
                }
                System.out.println("返回信息:" + bufferRes.toString());
                in.close();
                if (conn != null) {
                        // 关闭连接
                        conn.disconnect();
                }
        } catch (Exception e) {
                e.printStackTrace();
        }*/
        try {    
            String result = http.sendPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ accessToken,params);
            System.out.println("返回信息:" + result);
        } catch (Exception e) {
            e.printStackTrace();

    }


请求:

public static void main(String args[]){

String tinfo = "{\"touser\":\"收信息的openid\",\"template_id\":\"模版ID\",\"url\":\"http://www.test.com.cn/testN.jsp\"," +
                    "\"data\":{\"first\":{\"value\":\"test\"},\"keyword1\":{\"value\":\"孩子的视频\"},\"keyword2\":{\"value\":\"20140923\"},\"remark\":{\"value\":\"sfsfsfsf\"}}}";
        createTemplate(tinfo,ACCESS_TOKEN);

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值