微信公众平台开发3-Https请求的封装

来源:互联网 发布:淘宝卖家售后服务流程 编辑:程序博客网 时间:2024/05/12 03:06

转载请注明:http://blog.csdn.net/ly20116/article/details/51082999

根据开发文档,微信公众平台开发接口的调用都需要使用https协议。 
因此我们需要封装一个通用的https请求方法。 
该方法必须具备以下特点: 
1、使用https协议 
2、支持GET、POST请求方式 
3、支持有参数提交和无参数提交

步骤: 
1、实现证书信任管理器类 MyX509TrustManager,继承至X509TrustManager接口

/** * 证书信任管理类 * @author 洋 * */public class MyX509TrustManager implements X509TrustManager{    /**     * 检查客户端的证书,若不信任该证书则抛出异常     */    @Override    public void checkClientTrusted(X509Certificate[] chain, String authType)            throws CertificateException {        // TODO Auto-generated method stub    }    /**     * 检查服务器的证书,若不信任该证书同样抛出异常,如果是空函数体,则信任所有证书     */    @Override    public void checkServerTrusted(X509Certificate[] chain, String authType)            throws CertificateException {        // TODO Auto-generated method stub    }    /**     * 返回受信任的X509证书数组。     */    @Override    public X509Certificate[] getAcceptedIssuers() {        // TODO Auto-generated method stub        return null;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

2、实现https请求方法

这里写图片描述 
部分JSSE类关系图

/** * https工具类 * @author 洋 * */public class HttpsUtil {/**     * 发送Https请求并获取结果     * @param requestUrl     * @param method     * @param data     * @return     */    public static JSONObject httpsRequest(String requestUrl,String method,String data){        JSONObject jsonObject=null;        StringBuffer sb=new StringBuffer();        try {        //创建SSLContext对象,并使用我们指定的信任管理器初始化        TrustManager[] tm={new MyX509TrustManager()};        SSLContext sslContext= SSLContext.getInstance("SSL", "SunJSSE");        sslContext.init(null, tm, new SecureRandom());        //获取SSLSocketFactory对象        SSLSocketFactory ssf=sslContext.getSocketFactory();        //创建HttpsURLConnection对象,并设置其SSLSocketFactory对象        URL url=new URL(requestUrl);        HttpsURLConnection conn=(HttpsURLConnection) url.openConnection();        conn.setSSLSocketFactory(ssf);        //设置连接参数        conn.setDoInput(true);        conn.setDoOutput(true);        conn.setUseCaches(false);        conn.setRequestMethod(method);//设置请求方式        if(method.equals("GET")){            conn.connect();        }        if(data!=null){            OutputStream os=conn.getOutputStream();            os.write(data.getBytes("utf-8"));            os.close();        }        InputStream is=conn.getInputStream();        InputStreamReader isr=new InputStreamReader(is,"utf-8");        BufferedReader br=new BufferedReader(isr);        String str=null;        while((str=br.readLine())!=null){            sb.append(str);        }        //清理        br.close();        isr.close();        is.close();        is=null;        conn.disconnect();        //转化为JSON对象        jsonObject=JSONObject.fromObject(sb.toString());        } catch (ConnectException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return jsonObject;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

由于需要转换为JSONObject对象,且整个工程中也需要解析和生成JSON数据,所以需要到开源的jar包,本文导入的是json_lib工具包,也可以下载其他的。

阅读全文
0 0
原创粉丝点击