https与http的通信,在我看来主要的区别在于https多了一个安全验证机制,而Android采用的是X509验证,首先我们需要这重写X509类,建立我们的验证规则、、不过对于特定的项目,我们一般都是无条件信任服务端的,因此我们可以对任何证书都无条件信任(其实本质上我们只是信任了特定url的证书,为了偷懒,才那么选择的)/**
- * 信任所有主机-对于任何证书都不做检查
- */
- class MytmArray implements X509TrustManager {
- public X509Certificate[] getAcceptedIssuers() {
-
- return new X509Certificate[] {};
- }
-
- @Override
- public void checkClientTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
-
-
- }
-
- @Override
- public void checkServerTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
-
-
-
- }
- };
- * 信任所有主机-对于任何证书都不做检查
- */
- class MytmArray implements X509TrustManager {
- public X509Certificate[] getAcceptedIssuers() {
-
- return new X509Certificate[] {};
- }
-
- @Override
- public void checkClientTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
-
-
- }
-
- @Override
- public void checkServerTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
-
-
-
- }
- };
好了,我们写好了信任规则,接下载就要创建一个主机的信任列表
- static TrustManager[] xtmArray = new MytmArray[] { new MytmArray() };
-
-
-
-
- private static void trustAllHosts() {
-
-
-
- try {
- SSLContext sc = SSLContext.getInstance("TLS");
- sc.init(null, xtmArray, new java.security.SecureRandom());
- HttpsURLConnection
- .setDefaultSSLSocketFactory(sc.getSocketFactory());
-
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
-
-
-
- return true;
- }
- };
- static TrustManager[] xtmArray = new MytmArray[] { new MytmArray() };
-
-
-
-
- private static void trustAllHosts() {
-
-
-
- try {
- SSLContext sc = SSLContext.getInstance("TLS");
- sc.init(null, xtmArray, new java.security.SecureRandom());
- HttpsURLConnection
- .setDefaultSSLSocketFactory(sc.getSocketFactory());
-
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
-
-
-
- return true;
- }
- };
上面的都是https通信需要做的几个基本要求,接下载我们要做的就是https的使用啦下面就以get和post为例进行说明,中间还涉及到cookie的使用
- String httpUrl="XXXXX"
- String result = "";
- HttpURLConnection http = null;
- URL url;
- try {
- url = new URL(httpUrl);
-
- if (url.getProtocol().toLowerCase().equals("https")) {
- trustAllHosts();
- http = (HttpsURLConnection) url.openConnection();
- ((HttpsURLConnection) http).setHostnameVerifier(DO_NOT_VERIFY);
-
- } else {
- http = (HttpURLConnection) url.openConnection();
- }
- http.setConnectTimeout(10000);
- http.setReadTimeout(50000);
- http.setRequestMethod("GET");
- http.setDoInput(true);
- http.setRequestProperty("Content-Type", "text/xml");
-
- BufferedReader in = null;
- if (obj.getHttpStatus() == 200) {
- getCookie(http);
- in = new BufferedReader(new InputStreamReader(
- http.getInputStream()));
- } else
- in = new BufferedReader(new InputStreamReader(
- http.getErrorStream()));
- result = in.readLine();
- Log.i("result", result);
- in.close();
- http.disconnect();
- String httpUrl="XXXXX"
- String result = "";
- HttpURLConnection http = null;
- URL url;
- try {
- url = new URL(httpUrl);
-
- if (url.getProtocol().toLowerCase().equals("https")) {
- trustAllHosts();
- http = (HttpsURLConnection) url.openConnection();
- ((HttpsURLConnection) http).setHostnameVerifier(DO_NOT_VERIFY);
-
- } else {
- http = (HttpURLConnection) url.openConnection();
- }
- http.setConnectTimeout(10000);
- http.setReadTimeout(50000);
- http.setRequestMethod("GET");
- http.setDoInput(true);
- http.setRequestProperty("Content-Type", "text/xml");
-
- BufferedReader in = null;
- if (obj.getHttpStatus() == 200) {
- getCookie(http);
- in = new BufferedReader(new InputStreamReader(
- http.getInputStream()));
- } else
- in = new BufferedReader(new InputStreamReader(
- http.getErrorStream()));
- result = in.readLine();
- Log.i("result", result);
- in.close();
- http.disconnect();
https或http的get请求写好了,哦中间涉及到了一个getCookie的方法,如下:
-
- private static void getCookie(HttpURLConnection http) {
- String cookieVal = null;
- String key = null;
- DataDefine.mCookieStore = "";
- for (int i = 1; (key = http.getHeaderFieldKey(i)) != null; i++) {
- if (key.equalsIgnoreCase("set-cookie")) {
- cookieVal = http.getHeaderField(i);
- cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
- DataDefine.mCookieStore = DataDefine.mCookieStore + cookieVal
- + ";";
- }
- }
- }
-
- private static void getCookie(HttpURLConnection http) {
- String cookieVal = null;
- String key = null;
- DataDefine.mCookieStore = "";
- for (int i = 1; (key = http.getHeaderFieldKey(i)) != null; i++) {
- if (key.equalsIgnoreCase("set-cookie")) {
- cookieVal = http.getHeaderField(i);
- cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
- DataDefine.mCookieStore = DataDefine.mCookieStore + cookieVal
- + ";";
- }
- }
- }
public static Query HttpQueryReturnClass(String httpUrl, String base64) {
- String result = "";
- Log.i("控制", httpUrl);
- Query obj = new Query();
- HttpURLConnection http = null;
- URL url;
- try {
- url = new URL(httpUrl);
-
- if (url.getProtocol().toLowerCase().equals("https")) {
- trustAllHosts();
- http = (HttpsURLConnection) url.openConnection();
- ((HttpsURLConnection) http).setHostnameVerifier(DO_NOT_VERIFY);
- } else {
- http = (HttpURLConnection) url.openConnection();
- }
- http.setConnectTimeout(10000);
- http.setReadTimeout(50000);
- http.setRequestMethod("POST");
- http.setDoInput(true);
- http.setDoOutput(true);
- http.setRequestProperty("Content-Type", "text/xml");
- http.setRequestProperty("Cookie", DataDefine.mCookieStore);
- DataOutputStream out = new DataOutputStream(http.getOutputStream());
- out.writeBytes(base64);
- out.flush();
- out.close();
- obj.setHttpStatus(http.getResponseCode());
- BufferedReader in = null;
- if (obj.getHttpStatus() == 200) {
- getCookie(http);
- in = new BufferedReader(new InputStreamReader(
- http.getInputStream()));
- } else
- in = new BufferedReader(new InputStreamReader(
- http.getErrorStream()));
- result = in.readLine();
- in.close();
- http.disconnect();
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- String result = "";
- Log.i("控制", httpUrl);
- Query obj = new Query();
- HttpURLConnection http = null;
- URL url;
- try {
- url = new URL(httpUrl);
-
- if (url.getProtocol().toLowerCase().equals("https")) {
- trustAllHosts();
- http = (HttpsURLConnection) url.openConnection();
- ((HttpsURLConnection) http).setHostnameVerifier(DO_NOT_VERIFY);
- } else {
- http = (HttpURLConnection) url.openConnection();
- }
- http.setConnectTimeout(10000);
- http.setReadTimeout(50000);
- http.setRequestMethod("POST");
- http.setDoInput(true);
- http.setDoOutput(true);
- http.setRequestProperty("Content-Type", "text/xml");
- http.setRequestProperty("Cookie", DataDefine.mCookieStore);
- DataOutputStream out = new DataOutputStream(http.getOutputStream());
- out.writeBytes(base64);
- out.flush();
- out.close();
- obj.setHttpStatus(http.getResponseCode());
- BufferedReader in = null;
- if (obj.getHttpStatus() == 200) {
- getCookie(http);
- in = new BufferedReader(new InputStreamReader(
- http.getInputStream()));
- } else
- in = new BufferedReader(new InputStreamReader(
- http.getErrorStream()));
- result = in.readLine();
- in.close();
- http.disconnect();
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
这里面的base64是我经过base64加密过以后的数据