问题遇到的现象和发生背景
JAVA socket通过GET请求访问https网页(豆瓣读书),没有响应包,怎么解决
问题相关代码,请勿粘贴截图
java
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class HTTPClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader in = null;
PrintWriter out = null;
FileWriter fw = null;
try {
// www.book.douban.com
Socket socket = SSLSocketFactory.getDefault().createSocket("140.143.177.206", 443);
//Socket socket = new Socket("www.book.douban.com", 80);
if (socket.isConnected()) {
System.out.println(socket.getInetAddress());
System.out.println("客户端已经连上");
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),StandardCharsets.UTF_8),true);
out.write("GET / HTTP/1.1\r\n");
out.write("Host: 140.143.177.206\r\n");
out.write("Content-Type: text/html\r\n");
//out.write("Accept-Language: zh-CN\r\n");
//out.write("Cache-Control: max-age=0\r\n");
//out.write("User-agent:Mozilla/5.0\r\n");
out.write("Connection: keep-alive\r\n\r\n");
//out.write("\r\n");
out.flush();
System.out.println("连接成功!");
File file = new File("hello.html");
fw = new FileWriter(file);
String lineStr ;
while ((lineStr = in.readLine()) != null) {
System.out.println(lineStr);
fw.write(lineStr);
}
System.out.println("连接成功!");
}
}catch(Exception e){
e.printStackTrace();
} finally{
try {
if (out != null) {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
if(fw != null)
{
try{
fw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
运行结果及报错内容
运行结果
java
/140.143.177.206
客户端已经连上
连接成功!
连接成功!
我的解答思路和尝试过的方法
是我的GET写错了吗?还是那个地方有问题,求指出,感谢,第一次学Java语言,大哭
我想要达到的结果
想要:https的响应头和响应体