私はいくつかの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();
}