package com.android.settings;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
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://service.rm.magicyo.com";
HttpsURLConnection connection = this.getHttpsURLConnection(uri);
JSONObject respJo =null;
// Device SN
String deviceCode = TelephonyManagerReflect.getSN(context);
String accessToken="";
try {
accessToken = AESCipher.aesEncryptString(deviceCode,"*&#o5Dw$e%2~mZ[u");
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;
}