java https连接_java-HTTP / HTTPS客户端,对HTTPS请求进行“连接...

博主在尝试使用Java创建一个简单的HTTP/HTTPS客户端。当访问HTTP网站如www.google.com:80时,程序运行正常并获取了HTML内容。然而,尝试访问HTTPS网站如www.google.com:443或www.facebook.com:443时,遇到了连接重置的SocketException。问题可能涉及到HTTPS证书、SSL/TLS配置或网络安全设置。
摘要由CSDN通过智能技术生成

我正在尝试使用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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值