客户端 - 服务器应用Java

时间:2015-11-16 17:15:12

标签: java sockets http http-headers httprequest

我写了一个服务器类,我的客户端是我的浏览器。当我在浏览器中输入localhost:8082时,会打开硬编码的网站www.mmix.cs.hm.edu。到现在为止还挺好。 一个网站通常有多个页面。我的服务器只能检索主页www.mmix.cs.hm.edu/index.html,无论我是否点击其他链接。我希望能够导航到其他这些页面。任何人都可以看看我的代码并给我一个关于我如何进行的提示吗?

    public static void main(String args[]) {
    String fromClient = "www.mmix.cs.hm.edu";

    try(ServerSocket welcomeSocket = new ServerSocket(8082)){
        System.out.println("Server started, waiting for clients...");
        while(true){
            StringBuilder htmlCode = new StringBuilder();
            try(Socket connectionSocket = welcomeSocket.accept();
                    DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream());
                    BufferedReader fromBrowser = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()))){
                    try(InputStream url = new URL("http://"+fromClient+"/index.html").openStream();
                            BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){
                            for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){
                                htmlCode.append(line);
                            }
                            String str = htmlCode.toString();
                            toClient.writeBytes(str);
                                //toClient.write("\r\n");
                    }
            }
        }
    }
    catch(IOException io){
        io.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

@ ObiWanKenobi-更改了您的代码以提取URL部分。请尝试以下代码段。请通过代码段中的注释。运行并确认字符串操作是否有效。感谢。

public static void main(String args[]) {
        String fromClient = "www.mmix.cs.hm.edu";

        try(ServerSocket welcomeSocket = new ServerSocket(8082)){
            System.out.println("Server started, waiting for clients...");
            while(true){
                StringBuilder htmlCode = new StringBuilder();
                try(Socket connectionSocket = welcomeSocket.accept();
                        DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream());
                        BufferedReader fromBrowser = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()))){

                        String line1 = fromBrowser.readLine(); //Line 1 is of format: GET /index.html HTTP/1.1
                        String dynUrl = line1.substring(line1.indexOf(32)+1,line1.lastIndexOf(32)); //dynUrl is of format:/index.html
                        //Please note that the query string parameters not taken into account and the code may fail if the query string contains space character.
                        //Construct a new URL based on dynUrl
                        try(InputStream url = new URL("http://"+fromClient+dynUrl).openStream();
                                BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){
                                for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){
                                    htmlCode.append(line);
                                }
                                String str = htmlCode.toString();
                                toClient.writeBytes(str);
                                    //toClient.write("\r\n");
                        }
                }
            }
        }
        catch(IOException io){
            io.printStackTrace();
        }
    }