客户端-服务器应用程序 Java
Client-Server application Java
我写了一个Server Class,我的客户端就是我的浏览器。当我在浏览器中输入 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();
}
}
@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();
}
}
我写了一个Server Class,我的客户端就是我的浏览器。当我在浏览器中输入 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();
}
}
@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();
}
}