我想阅读一些 HTTP Requestst 并拥有这个:
public class HTTPServerThread extends Thread {
private Socket socket = null;
private BufferedOutputStream out;
public HTTPServerThread(Socket socket) {
super("HTTPServerThread");
this.socket = socket;
}
public void run() {
try {
out = new BufferedOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(new DataInputStream(socket.getInputStream())));
String request = "";
String temp;
while ((temp = in.readLine()) != null) {
request += temp+"\n";
System.out.println("request:\n"+request);
}
System.out.println("request final:\n"+request);
if (request.equals("")) System.out.println("Request empty");
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当我运行我的代码并在我的服务器上请求某些内容时,请求会在 while 循环中部分打印出来,所以一切正常。但是当我最终打印出来时,它给了我“”,所以打印出“请求为空”,我不知道我的(可能是愚蠢的)错误是什么,所以请帮助我:)
电流输出:
request:
GET / HTTP/1.1
request:
GET / HTTP/1.1
Host: localhost
request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de,en-US;q=0.8,en;q=0.6
request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de,en-US;q=0.8,en;q=0.6
request final:
Request empty
主要课程:
public class HTTPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
int port;
boolean listening = true;
//read port from args[0] if exists, else port = 80
if (args.length == 0) {
port = 80;
}
else {
port = Integer.parseInt(args[0]);
}
//start listening on port
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e) {
System.out.println("Could not listen on port: "+port);
System.exit(-1);
}
System.out.println("Java HTTP-Server");
System.out.println("Port: " + port);
//start new Thread if someone wants to connect
while (listening)
new HTTPServerThread(serverSocket.accept()).start();
serverSocket.close();
}