具有 Java 个套接字的 HTTP 客户端
HTTP Client with Java sockets
我尝试在 Java 中打开一个 TCP 套接字并向服务器发送 GET 请求 (www.abc.net.au) 并将响应打印到控制台。
客户代码:
import java.util.Scanner;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Client {
public static void main(String[] args) throws Exception{
Scanner sc;
String addressString;
int port =80;
int timeoutMs = 30000;
String temp;
//Read address from commandline
sc =new Scanner(System.in);
System.out.print("Adresse: ");
addressString = sc.next();
sc.close();
//Open socket
InetAddress addr = InetAddress.getByName(addressString);
SocketAddress sockaddr = new InetSocketAddress(addr, port);
Socket socket = new Socket();
//Connection timeout
socket.connect(sockaddr,timeoutMs);
System.out.println( socket.getPort() +"\n");
//Define input/output
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
//Send GET request
pw.print("GET / HTTP/1.1\r\n");
pw.print("Host: "+ addressString +"\r\n\r\n");
while((temp=br.readLine())!=null){
System.out.println(temp);
}
//Close Input/Output and Socket
pw.close();
br.close();
socket.close();
}
}
该代码似乎一直有效,直到它到达 while 之后我得到:
HTTP/1.0 408 Request Time-out
Server: AkamaiGHost
Mime-Version: 1.0
Date: Sun, 07 Oct 2018 15:36:40 GMT
Content-Type: text/html
Content-Length: 218
Expires: Sun, 07 Oct 2018 15:36:40 GMT
<HTML><HEAD>
<TITLE>Request Timeout</TITLE>
</HEAD><BODY>
<H1>Request Timeout</H1>
The server timed out while waiting for the browser's request.<P>
Reference #2.3d5b6068.1538926600.0
</BODY></HTML>
Process finished with exit code 0
我不明白是什么导致了这个问题,已经在 Whosebug、java2s 和我家里的一本书上寻找答案。尝试其他页面通常以以下结尾:
Process finished with exit code 0
仅此而已。
知道我的请求 missing/incorrect 是什么吗?
感谢每一个提示。
PrintWriter
具有用于存储数据的内部缓冲区,因此当您调用 print
时 - 不会向服务器发送任何内容
来自https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output.
因此,您可以使用这些方法代替 print
,或者直接调用 flush
:
pw.print("GET / HTTP/1.1\r\n");
pw.print("Host: " + addressString + "\r\n\r\n");
pw.flush(); // actually send data to server
我尝试在 Java 中打开一个 TCP 套接字并向服务器发送 GET 请求 (www.abc.net.au) 并将响应打印到控制台。
客户代码:
import java.util.Scanner;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Client {
public static void main(String[] args) throws Exception{
Scanner sc;
String addressString;
int port =80;
int timeoutMs = 30000;
String temp;
//Read address from commandline
sc =new Scanner(System.in);
System.out.print("Adresse: ");
addressString = sc.next();
sc.close();
//Open socket
InetAddress addr = InetAddress.getByName(addressString);
SocketAddress sockaddr = new InetSocketAddress(addr, port);
Socket socket = new Socket();
//Connection timeout
socket.connect(sockaddr,timeoutMs);
System.out.println( socket.getPort() +"\n");
//Define input/output
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
//Send GET request
pw.print("GET / HTTP/1.1\r\n");
pw.print("Host: "+ addressString +"\r\n\r\n");
while((temp=br.readLine())!=null){
System.out.println(temp);
}
//Close Input/Output and Socket
pw.close();
br.close();
socket.close();
}
}
该代码似乎一直有效,直到它到达 while 之后我得到:
HTTP/1.0 408 Request Time-out
Server: AkamaiGHost
Mime-Version: 1.0
Date: Sun, 07 Oct 2018 15:36:40 GMT
Content-Type: text/html
Content-Length: 218
Expires: Sun, 07 Oct 2018 15:36:40 GMT
<HTML><HEAD>
<TITLE>Request Timeout</TITLE>
</HEAD><BODY>
<H1>Request Timeout</H1>
The server timed out while waiting for the browser's request.<P>
Reference #2.3d5b6068.1538926600.0
</BODY></HTML>
Process finished with exit code 0
我不明白是什么导致了这个问题,已经在 Whosebug、java2s 和我家里的一本书上寻找答案。尝试其他页面通常以以下结尾:
Process finished with exit code 0
仅此而已。
知道我的请求 missing/incorrect 是什么吗?
感谢每一个提示。
PrintWriter
具有用于存储数据的内部缓冲区,因此当您调用 print
时 - 不会向服务器发送任何内容
来自https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output.
因此,您可以使用这些方法代替 print
,或者直接调用 flush
:
pw.print("GET / HTTP/1.1\r\n");
pw.print("Host: " + addressString + "\r\n\r\n");
pw.flush(); // actually send data to server