使用Socket连接到https

时间:2014-03-28 08:46:14

标签: java android ssl

我的应用程序基于套接字,但是常规套接字,而不是SSLSockets。我是否必须将所有这些更改为SSL套接字才能连接到https服务器?

2 个答案:

答案 0 :(得分:1)

您必须使用SSLSocketFactory更改套接字的创建方式,但您不必将所有数据类型从Socket更改为SSLSocket。

答案 1 :(得分:0)

是的我认为您必须更改SSL,您可以尝试:

System.setProperty("javax.net.ssl.trustStore", "clienttrust");
    SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket s = ssf.createSocket("127.0.0.1", 8888);

    OutputStream outs = s.getOutputStream();
    PrintStream out = new PrintStream(outs);
    InputStream ins = s.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(ins));

    out.println("Hi,How are u!");
    out.println("");
    String line = null;
    while ((line = in.readLine()) != null) {
      System.out.println(line);
    }

    in.close();
    out.close();

来自http://www.java2s.com/Tutorial/Java/0490__Security/HttpsSocketClient.htm

希望它有所帮助!