我编写了一个使用端口8082来检索网页的服务器,然后我可以通过在浏览器中输入localhost:8082
来访问该网页。我遇到的问题是,当我点击网站上的其他资源时,我会收到Chrome浏览器ERR_EMPTY_RESPONSE
消息。
有人可以告诉我如何配置我的代码,以便我不仅可以访问基本网址,还可以访问该网站上引用的其他网址?
这是我的代码:
public static void main(String[] args) {
StringBuilder htmlCode = new StringBuilder();
String hostUrl = "www.mmix.cs.hm.edu";
try(ServerSocket welcomeSocket = new ServerSocket(8082)) {
System.out.println("Server connected...waiting for client");
try(Socket connectionSocket = welcomeSocket.accept();
DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
InputStream url = new URL("http://"+hostUrl+"/index.html").openStream();
BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){
for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){
htmlCode = htmlCode.append(line);
}
String html = htmlCode.toString();
toClient.writeBytes(html);
}
}
catch(IOException io){
io.printStackTrace();
}
}