微信开发

取token

URL url = new URL(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ appid + "&secret=" + appSecret);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,
"UTF-8"));
StringBuilder sb = new StringBuilder("");
String res;
while ((res = reader.readLine()) != null) {
sb.append(res.trim());
}
String[] arr1 = sb.toString().split(",");
String[] arr2 = arr1[0].split(":");
token = arr2[1].replace("\"", "");

上传素材(图片、语音):

URL url = new URL(
"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false); // post方式不能使用缓存
        // 设置请求头信息
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Charset", "UTF-8");
        // 设置边界
        String BOUNDARY = "----------" + System.currentTimeMillis();
        con.setRequestProperty("Content-Type",
                        "multipart/form-data; boundary="
                        + BOUNDARY);
        // 请求正文信息
        // 第一部分:
        StringBuilder sb = new StringBuilder();
        sb.append("--"); // 必须多两道线
        sb.append(BOUNDARY);
        sb.append("\r\n");
        sb.append("Content-Disposition: form-data;name=\"media\";filename=\""
                        + file.getOriginalFilename() + "\" \r\n");
        sb.append("Content-Type:application/octet-stream\r\n\r\n");
        byte[] head = sb.toString().getBytes("utf-8");
        // 获得输出流
        OutputStream out = new DataOutputStream(con.getOutputStream());
        // 输出表头
        out.write(head);
        // 文件正文部分
        // 把文件以流文件的方式 推入到url中
File fi = new File(file.getOriginalFilename());
file.transferTo(fi);
        DataInputStream in = new DataInputStream(new FileInputStream(fi));
        int bytes = 0;
        byte[] bufferOut = new byte[1024];
        while ((bytes = in.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
        }
        in.close();
        // 结尾部分
        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
        out.write(foot);
        out.flush();
        out.close();
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = null;
        // 定义BufferedReader输入流来读取URL的响应
        reader = new BufferedReader(new InputStreamReader(con
        .getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
                buffer.append(line);
        }
        if (result == null) {
                result = buffer.toString();
        }
return result;

上传素材(视频):

URL url = new URL(
"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false); // post方式不能使用缓存
        // 设置请求头信息
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Charset", "UTF-8");
        // 设置边界
        String BOUNDARY = "----------" + System.currentTimeMillis();
        con.setRequestProperty("Content-Type",
                        "multipart/form-data; boundary="
                        + BOUNDARY);
        // 请求正文信息
        // 第一部分:
        StringBuilder sb = new StringBuilder();
        sb.append("--"); // 必须多两道线
        sb.append(BOUNDARY);
        sb.append("\r\n");
        sb.append("Content-Disposition: form-data;name=\"description\" \r\n\r\n");   
        sb.append(description+"\r\n"); 
        sb.append("--"); // 必须多两道线
        sb.append(BOUNDARY);
        sb.append("\r\n");
        sb.append("Content-Disposition: form-data;name=\"media\";filename=\""
                        + file.getOriginalFilename() + "\" \r\n"); 
        sb.append("Content-Type:application/octet-stream\r\n\r\n");
        byte[] head = sb.toString().getBytes("utf-8");
        // 获得输出流
        OutputStream out = new DataOutputStream(con.getOutputStream());
        // 输出表头
        out.write(head);
        // 文件正文部分
        // 把文件以流文件的方式 推入到url中
File fi = new File(file.getOriginalFilename());
file.transferTo(fi);
        DataInputStream in = new DataInputStream(new FileInputStream(fi));
        int bytes = 0;
        byte[] bufferOut = new byte[1024];
        while ((bytes = in.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
        }
        in.close();
        // 结尾部分
        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
        out.write(foot);
        out.flush();
        out.close();
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = null;
        // 定义BufferedReader输入流来读取URL的响应
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
                buffer.append(line);
        }
        if (result == null) {
                result = buffer.toString();
        }
return result;

下载图片素材

URL url = new URL(
"https://api.weixin.qq.com/cgi-bin/material/get_material?access_token="+token);
URLConnection connection = url.openConnection();
//post
connection.setDoOutput(true);
connection.setDoInput(true);
// 获取URLConnection对象对应的输出流
        out = new PrintWriter(connection.getOutputStream());
        // 发送请求参数
        out.print(BeanUtility.objectToJsonStr(params));
        // flush输出流的缓冲
        out.flush();
InputStream in = connection.getInputStream();
BufferedImage img = ImageIO.read(in);
if(img != null){
img.flush();
}
return img;

下载音频素材:

URL url = new URL(
"https://api.weixin.qq.com/cgi-bin/material/get_material?access_token="+token);
URLConnection connection = url.openConnection();
//post
connection.setDoOutput(true);
connection.setDoInput(true);
// 获取URLConnection对象对应的输出流
        out = new PrintWriter(connection.getOutputStream());
        // 发送请求参数
        out.print(BeanUtility.objectToJsonStr(params));
        // flush输出流的缓冲
        out.flush();
        InputStream in = connection.getInputStream();
byte[] b = new byte[1024];
while (in.read(b) != -1) {
os.write(b);
}

下载视频素材:

URL url = new URL(
"https://api.weixin.qq.com/cgi-bin/material/get_material?access_token="+token);
URLConnection connection = url.openConnection();
//post
connection.setDoOutput(true);
connection.setDoInput(true);
// 获取URLConnection对象对应的输出流
        out = new PrintWriter(connection.getOutputStream());
        String datas = BeanUtility.objectToJsonStr(params);
        // 发送请求参数
        out.print(datas);
        // flush输出流的缓冲
        out.flush();
        InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder("");
String res;
while ((res = reader.readLine()) != null) {
sb.append(res.trim());
}
return sb.toString();

取JS-SDK使用权限签名jsapi_ticket

URL url = new URL(
"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="
+ token + "&type=jsapi");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,
"UTF-8"));
StringBuilder sb = new StringBuilder("");
String res;
while ((res = reader.readLine()) != null) {
sb.append(res.trim());
}
String resultStr = sb.toString();
JSONObject jsonObject = JSONObject.fromObject(resultStr);
Map<String, Object> dataMap = (Map<String, Object>) JSONObject.toBean(
jsonObject, Map.class);
String jsapi_ticket = "";
int errcode = (Integer) dataMap.get("errcode");
if (errcode == 0) {
jsapi_ticket = (String) dataMap.get("ticket");
} else {
jsapi_ticket = "error";
}
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
String taskUrl = "http://" + host + "/wogp/init_run.html?openId="+
openId + "&dragonId="+ dragonId +"&parentId=" +parentId
+"&code=" + code + "&state=" + state;
String signature = "";
// 注意这里参数名必须全部小写,且必须有序
String string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr="
+ nonce_str + "&timestamp=" + timestamp + "&url=" + taskUrl;
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("signature", signature);
resultMap.put("nonceStr", nonce_str);
resultMap.put("timestamp", timestamp);
resultMap.put("appid", appid);
jsonObj = JSONObject.fromObject(resultMap);
return jsonObj;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值