公告:1.恭喜本站微信硬件蓝牙教程阅读总数突破100万次,微信jsapi阅读总数突破10万次... 2.友情交换/给本站留言

案例归档:  微信硬件开发设备授权—实现代码

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
/****
 * 
 * @author www.vxzsk.com V型知识库
 *
 */
public class DeviceAuthTest {
    /**
     * V型知识库
     * 模拟post方法
     */
     public static String sendPost(String requrl,String param){
         URL url;
          String sTotalString="";  
        try {
            url = new URL(requrl);
             URLConnection connection = url.openConnection(); 
              
             connection.setRequestProperty("accept", "*/*");
             connection.setRequestProperty("connection", "Keep-Alive");
             connection.setRequestProperty("Content-Type", "text/xml");
            // connection.setRequestProperty("Content-Length", body.getBytes().length+"");
             connection.setRequestProperty("User-Agent",
                     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
              
              
                connection.setDoOutput(true);  
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");  
                out.write(param); // 向页面传递数据。post的关键所在!  
                out.flush();  
                out.close();  
                // 一旦发送成功,用以下方法就可以得到服务器的回应:  
                String sCurrentLine;  
               
                sCurrentLine = "";  
                sTotalString = "";  
                InputStream l_urlStream;  
                l_urlStream = connection.getInputStream();  
                // 传说中的三层包装阿!  
                BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
                        l_urlStream));  
                while ((sCurrentLine = l_reader.readLine()) != null) {  
                    sTotalString += sCurrentLine + "\r\n";  
           
                }  
                 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
            
            System.out.println(sTotalString);  
            return sTotalString;
     }
 
    public static void main(String[] args) {
        String access_token="";//更换为自己的access_token
        String deviceId="";//调用生成二维码接口返回的设备id
        String mac="";//设备mac地址,可询问提供硬件设备的厂商人员
        String params="{\"device_num\":\"1\",\"device_list\":[{"
                   +"\"id\":\""+deviceId+"\","
                   +"\"mac\":\""+mac+"\","
                    +"\"connect_protocol\":\"3\","
                    +"\"auth_key\":\"\","
                    +"\"close_strategy\":\"1\","
                    +"\"conn_strategy\":\"1\","
                    +"\"crypt_method\":\"0\","
                    +"\"auth_ver\":\"0\","
                    +"\"manu_mac_pos\":\"-1\","
                    +"\"ser_mac_pos\":\"-2\","
                    +"\"ble_simple_protocol\": \"0\""
                    + "}],"
                    +"\"op_type\":\"1\""
                   + "}";
                   
              String s=DeviceAuthTest.sendPost("https://api.weixin.qq.com/device/authorize_device?access_token="+access_token, params);
                  System.out.println("返回:"+s);
    }
 
}