我正在尝试使用java创建一个简单的HTTP / HTTPS客户端.到目前为止,我在Client.java文件中所做的事情都在这里.
当我尝试访问www.google.com:80时,一切工作正常.我在响应BufferedReader中获得了完整的HTML内容.
但是,当我尝试访问www.google.com:443时,没有数据通过BufferedReader传入.
对于www.facebook.com:80,
HTTP/1.1 302 Found
另外,当我尝试使用www.facebook.com:443时,出现以下错误:
Exception in thread "main" java.net.SocketException: Connection reset
我要去哪里错了?为什么我无法获得HTTPS网站的任何响应?
public class Client {
public static void main(String[] args) throws IOException {
//String host = args[0];
//int port = Integer.parseInt(args[1]);
//String path = args[2];
int port = 80;
String host = "www.google.com";
String path = "/";
//Opening Connection
Socket clientSocket = new Socket(host, port);
System.out.println("======================================");
System.out.println("Connected");
System.out.println("======================================");
//Declare a writer to this url
PrintWriter request = new PrintWriter(clientSocket.getOutputStream(),true);
//Declare a listener to this url
BufferedReader response = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Sending request to the server
//Building HTTP request header
request.print("GET "+path+" HTTP/1.1
"); //"+path+"
request.print("Host: "+host+"
");
request.print("Connection: close
");
request.print("
");
request.flush();
System.out.println("Request Sent!");
System.out.println("======================================");
//Receiving response from server
String responseLine;
while ((responseLine = response.readLine()) != null) {
System.out.println(responseLine);
}
System.out.println("======================================");
System.out.println("Response Recieved!!");
System.out.println("======================================");
request.close();
response.close();
clientSocket.close();
}
}