[英]HTTP/HTTPS client, “connection reset” on HTTPS requests
I'm trying to make a simple HTTP/HTTPS client using java . 我正在尝试使用java创建一个简单的HTTP / HTTPS客户端 。 What i have done as of now in my Client.java file is here. 我现在在Client.java文件中所做的就是这里。
Everything is working good when i try to access www.google.com:80. 当我尝试访问www.google.com:80时,一切正常。 I'm getting the full HTML content in my response BufferedReader. 我在响应BufferedReader中获取完整的HTML内容。
But, when i try to access www.google.com:443, there is no data coming through BufferedReader 但是,当我尝试访问www.google.com:443时,BufferedReader没有数据
For www.facebook.com:80, 对于www.facebook.com:80,
HTTP/1.1 302 Found
Also, when i try with www.facebook.com:443, Getting the following error: 此外,当我尝试使用www.facebook.com:443时,出现以下错误:
Exception in thread "main" java.net.SocketException: Connection reset
Where am i going wrong? 我哪里错了? Why am i not able to get any response for the HTTPS sites? 为什么我无法获得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\r\n"); //"+path+"
request.print("Host: "+host+"\r\n");
request.print("Connection: close\r\n");
request.print("\r\n");
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();
}
}
HTTPS is encrypted; HTTPS已加密; it's HTTP via SSL. 它是通过SSL的HTTP。 You can't just send raw HTTP requests. 您不能只发送原始HTTP请求。 The server will drop your connection immediately if you start sending data without establishing a secure connection first (hence the connection reset error). 如果您在没有首先建立安全连接的情况下开始发送数据,则服务器将立即断开连接(因此连接重置错误)。 You have to establish an SSL connection first. 您必须首先建立SSL连接。 You'd want to use an SSLSocket
(via SSLSocketFactory
, see also example ) instead of a Socket
in that case. 在这种情况下,您需要使用SSLSocket
(通过SSLSocketFactory
,另请参见示例 )而不是Socket
。
It's as simple as changing one line of your code for the HTTPS case (well, three if you count the exception specification and the port number change to 443): 它就像为HTTPS情况更改代码的一行一样简单(如果计算异常规范并且端口号更改为443,则为三行):
Socket clientSocket = SSLSocketFactory.getDefault().createSocket(host, port);
Note that clientSocket
will be an instance of SSLSocket
(which is derived from Socket
) in this case. 请注意,在这种情况下, clientSocket
将是SSLSocket
的实例(从Socket
派生)。
However, if you're doing this as part of a larger application (as opposed to just a learning experience), consider existing libraries, such as Apache's HttpClient
(which supports HTTPS as well) or the built-in HttpURLConnection
and HttpsURLConnection
if you need something more basic. 但是,如果您将此作为更大应用程序的一部分(而不仅仅是学习经验),请考虑现有的库,例如Apache的HttpClient
(也支持HTTPS)或内置的HttpURLConnection
和HttpsURLConnection
如果需要)更基本的东西。 If you need to embed a server in an application you can use the built-in HttpServer
or HttpsServer
. 如果需要在应用程序中嵌入服务器,可以使用内置的HttpServer
或HttpsServer
。
Also issues that EJP mentioned in his comment . 也是EJP在评论中提到的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.