Java字符串为空虽然打印出来但之前没有空

时间:2013-06-28 15:46:11

标签: java string

我想阅读一些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

使用main的类:

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();
}

2 个答案:

答案 0 :(得分:1)

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

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

因此将停止条件更改为

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

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

答案 1 :(得分:-3)

你应该把一个ip和端口放到你的套接字

你的插座不知道去哪里

使用它:

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

相关问题