Android Https请求

package com.android.settings;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
 * Created by ybf on 2017/11/15.
 */
public  class DefaultTrustManager implements X509TrustManager {
    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }
    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

}
//ybf https ssl
        public  HttpsURLConnection getHttpsURLConnection(String uri) throws IOException {
        SSLContext ctx = null;
        try {
            ctx = SSLContext.getInstance("TLS");
            ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SSLSocketFactory ssf = ctx.getSocketFactory();
        URL url = new URL(uri);
        HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
        httpsConn.setSSLSocketFactory(ssf);
        httpsConn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
        httpsConn.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
        httpsConn.setDoInput(true);
        httpsConn.setDoOutput(true);
        return httpsConn;
    }
public JSONObject getUpgradeInfo() throws IOException {
        Log.e(TAG,"getUpgradeInfo");
        String uri = "https://com.baidu.com";
        HttpsURLConnection connection = this.getHttpsURLConnection(uri);
        JSONObject respJo =null;
        // Device SN 
        String deviceCode = TelephonyManagerReflect.getSN(context);
        String accessToken="";
        try {
             accessToken = AESCipher.aesEncryptString(deviceCode,"秘钥");
            Log.e(TAG,"accessToken -->"+accessToken);
        } catch (Exception e) {
            Log.e(TAG,"key Exception-->"+e.toString());
        }

        String  iVersion  ="Vzte_rm_http_1.0.0";
        String curVersion = SysProps.getDisplayID();
        JSONObject obj = new JSONObject();
        try {
            obj.put("accessToken",accessToken);
            obj.put("deviceCode",deviceCode);
            obj.put("iVersion",iVersion);
            obj.put("curVersion",curVersion);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String jsonObj =    obj.toString();

        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(jsonObj.getBytes("UTF-8"));//这样可以处理中文乱码问题
        out.flush();
        out.close();
        //读取响应
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String lines;
        StringBuffer sb = new StringBuffer("");
        while ((lines = reader.readLine()) != null) {
            lines = new String(lines.getBytes(), "utf-8");
            sb.append(lines);
        }
        Log.e(TAG,"返回结果:sb=="+ sb);
        reader.close();
        String  result = sb.toString();
        try {
            respJo = new JSONObject(result);
            int resCode = respJo.getInt("resCode");
            Log.e(TAG,"resCode=="+ resCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // 断开连接
        connection.disconnect();
    return respJo;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FW_G8Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值