Android:简单的HTTPS请求并获得响应

时间:2015-05-08 14:42:02

标签: java android sockets https connection

我正在创建一个Android代理服务器,我将其用于HTTP请求:

Socket socket = new Socket(InetAddress.getByName("perdu.com"), 80);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());

out.print("GET / HTTP/1.1\r\nHost: perdu.com\r\nConnection: close\r\n\r\n");
out.flush();

我不支持HTTPS请求,所以我的问题是:如何替换SSL工作并获得服务器响应?

谢谢

PS:我是法国人,对不起我的英语:)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(domain, 443);

socket.startHandshake();
BufferedReader in = new BufferedReader(new InpuStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());