3

我想阅读一些 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();
}
4

2 回答 2

1

正如JB Nizet猜测的那样,问题是由原始线程卡在while循环中引起的,而第二个线程实际上打印了空字符串。

循环的最后一次迭代while产生一个空的但不是空的temp字符串,从看似重复的请求输出中可以看出,在线程卡住之前有一个额外的空行。

所以将停止条件更改为

while ((temp = in.readLine()) != null && temp.length() > 0) {

将允许原始线程退出循环并打印正确的最终请求。

于 2013-06-28T16:42:45.273 回答
-3

你应该在你的套接字上放一个 ip 和端口

你的插座不知道去哪里

用这个:

Socket socket=new Socket(int ip , int port);

于 2013-06-28T15:54:05.713 回答