如何在我的android studio应用程序中实现TLS V1.1和V1.2?

问题描述:

我已经在我的主机上购买了SSL认证,但我不知道如何在我的java代码端(使用Android Studio)实现。如何在我的android studio应用程序中实现TLS V1.1和V1.2?

这是我TLSSocketFactory.java(我从另一个页面复制这个类,我把它放在我的Java代码)

import java.io.IOException; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.security.KeyManagementException; 
import java.security.NoSuchAlgorithmException; 

import javax.net.ssl.SSLContext; 
import javax.net.ssl.SSLSocket; 
import javax.net.ssl.SSLSocketFactory; 

public class TLSSocketFactory extends SSLSocketFactory { 

private SSLSocketFactory internalSSLSocketFactory; 

public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { 
    SSLContext context = SSLContext.getInstance("TLS"); 
    context.init(null, null, null); 
    internalSSLSocketFactory = context.getSocketFactory(); 
} 

@Override 
public String[] getDefaultCipherSuites() { 
    return internalSSLSocketFactory.getDefaultCipherSuites(); 
} 

@Override 
public String[] getSupportedCipherSuites() { 
    return internalSSLSocketFactory.getSupportedCipherSuites(); 
} 

@Override 
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { 
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose)); 
} 

@Override 
public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); 
} 

@Override 
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { 
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort)); 
} 

@Override 
public Socket createSocket(InetAddress host, int port) throws IOException { 
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); 
} 

@Override 
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { 
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort)); 
} 

private Socket enableTLSOnSocket(Socket socket) { 
    if(socket != null && (socket instanceof SSLSocket)) { 
     ((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"}); 
    } 
    return socket; 
} 
} 

这是我的方法使用get和post:

GET方法:

import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.net.URL; 
import java.nio.charset.Charset; 

public class JSONParser { 

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { 
    InputStream is = new URL(url).openStream(); 
    try { 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); 
     String jsonText = readAll(rd); 
     return new JSONObject(jsonText); 
    } finally { 
     is.close(); 
    } 
} 

private static String readAll(Reader rd) throws IOException { 
    StringBuilder sb = new StringBuilder(); 
    int cp; 
    while ((cp = rd.read()) != -1) { 
     sb.append((char) cp); 
    } 
    return sb.toString(); 
} 

} 

POT方法:

public String performPostCall(String requestURL, HashMap<String, String> postDataParams) { 

    URL url; 

    StringBuffer response = new StringBuffer(); 
    try { 
     url = new URL(requestURL); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(15000); 
     conn.setConnectTimeout(15000); 
     conn.setRequestMethod("POST"); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 


     OutputStream os = conn.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8")); 
     writer.write(getPostDataString(postDataParams)); 

     writer.flush(); 
     writer.close(); 
     os.close(); 
     int responseCode=conn.getResponseCode(); 

     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      String line; 
      BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); 

      while ((line=br.readLine()) != null) { 
       response.append(line); 
      } 
      br.close(); 
     } 
     else { 
      response.append(""); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return response.toString(); 
} 
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException { 
    StringBuilder result = new StringBuilder(); 
    boolean first = true; 
    for(Map.Entry<String, String> entry : params.entrySet()){ 
     if (first) 
      first = false; 
     else 
      result.append("&"); 

     result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); 
     result.append("="); 
     result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); 
    } 

    return result.toString(); 
} 

1.问题1:如何将我的TLSSocketFactory实例与我的POST和GET请求混合?对于我的noob问题抱歉。

2.问题2:我在我的网络服务中做了些什么?或者只是让我的Web服务与证书是好的?

谢谢!

你并不需要一个TLSSocketFactory,Android使用Apache的百科全书HTTP客户端来管理你的,看(Android - Sending HTTPS Get Request

如果从有效的实体,这意味着该证书链是有效的购买凭证,你不应该有任何问题。

我建议你阅读this

UPDATE我建议使用this library HTTP请求

+0

Anroid中的Apache公共客户端已被弃用。 – Robert

+0

我不知道,对不起:( – Driver

+0

更新了答案 – Driver

如果您的网址以https://开始返回的连接是HttpsUrlConnection上,你可以连接到之前设置使用套接字工厂服务器:

TLSSocketFactory myTlsSocketFactory = new TLSSocketFactory(); 
... 
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
conn.setSSLSocketFactory(myTlsSocketFactory); 
+0

感谢罗伯特,这是为我的POST方法,但为我的get方法吗? –