获取网络类的URL地址

时间:2014-08-14 19:56:33

标签: java sockets http webserver

我写了一个简单的代码,这是一个很小的Web服务器。我希望在客户端输入loalhost:8181/pic时显示图片但是当我的字符串str = in.readLine() str只显示localhost时:8181如何在浏览器中找到客户端进入localhost:8181/pic?有我简单的代码:

    protected void start() {
    ServerSocket s;

    System.out.println("Webserver starting up on port 80");
    System.out.println("(press ctrl-c to exit)");
    try {
        // create the main server socket
        s = new ServerSocket(8181);
    } catch (Exception e) {
        System.out.println("Error: " + e);
        return;
    }

    System.out.println("Waiting for connection");
    for (;;) {
        try {
            Socket remote = s.accept();
            System.out.println("Connection, sending data.");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    remote.getInputStream()));
            PrintWriter out = new PrintWriter(remote.getOutputStream());
            String method = in.readLine();
            out.flush();
            remote.close();
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

你需要更像这样的东西:

protected void start() {
    ServerSocket s;

    System.out.println("Webserver starting up on port 8181");
    System.out.println("(press ctrl-c to exit)");
    try {
        // create the main server socket
        s = new ServerSocket(8181);
    } catch (Exception e) {
        System.out.println("Error: " + e);
        return;
    }

    for (;;) {
        System.out.println("Waiting for connection");
        try {
            Socket remote = s.accept();
            System.out.println("Connection established.");
            BufferedReader in = new BufferedReader(new InputStreamReader(remote.getInputStream()));
            PrintWriter out = new PrintWriter(remote.getOutputStream());

            String tokens[] = in.readLine().split("\\s+") ;
            if (tokens.length != 3)
            {
                out.println("HTTP/1.0 400 Bad Request");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            if ((tokens[2].compareToIgnoreCase("HTTP/1.0") != 0) &&
                (tokens[2].compareToIgnoreCase("HTTP/1.1") != 0))
            {
                out.println("HTTP/1.0 505 HTTP Version Not Supported");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            if (tokens[0].compareToIgnoreCase("GET") != 0)
            {
                out.println("HTTP/1.0 405 Method Not Allowed");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            String path = tokens[1];
            String query = null;

            int idx = path.indexOf('?');
            if (idx != -1)
            {
                query = path.substring(idx+1);
                file = path.substring(0, idx);
            }

            if (path != "/pic")
            {
                out.println("HTTP/1.0 404 Not Found");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            out.println("HTTP/1.0 200 OK");
            out.println("Connection: close");
            out.println("Content-Type: ..."); // for you to fill in
            out.println("Content-Length: ..."); // for you to fill in
            out.println();

            // write out image data here...

            out.flush();
            remote.close();
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

答案 1 :(得分:0)

您需要检索所有可用的输入,而不仅仅是第一行:

String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}

这会给你这样的东西:

GET /pic?hello=true HTTP/1.1
Host: localhost:8181
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
DNT: 1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,af;q=0.6

现在,您可以看到浏览器实际请求的内容:

它要求服务器GET /pic?hello=true

带有一堆其他请求标题。

答案 2 :(得分:0)

你应该采取所有参数。不只是两个第一线;

    String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}