Java网络编程(模拟浏览器访问Tomcat服务器)

程序运行结果:

HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1026
Date: Mon, 28 Mar 2016 02:45:16 GMT
Connection: close

目前位置还没有找到解决方法,我的html文件确实存在于这个路径下,但就是找不到,

 1 package WebProgramingDemo;
 2 
 3 import java.io.InputStream;
 4 import java.io.PrintStream;
 5 import java.net.Socket;
 6 import java.net.UnknownHostException;
 7 
 8 public class MyBrowser {
 9 
10     /**
11      * @param args
12      * @throws Exception
13      * @throws UnknownHostException
14      */
15     // 模拟一个浏览器来获取服务器信息
16     public static void main(String[] args) throws Exception {
17         Socket s = new Socket("192.168.2.103", 8080);
18         PrintStream out = new PrintStream(s.getOutputStream());
19         out.println("GET /myweb/index.html HTTP/1.1");
20         out.println("Accept: */*");
21         out.println("Host: 192.168.2.103:8888");
22         out.println("Connection: close");
23         out.println();
24         out.println();
25         InputStream in = s.getInputStream();
26         byte[] buf = new byte[1024];
27         int len = in.read(buf);
28         System.out.println(new String(buf, 0, len));
29     }
30 
31 }

问题已经解决:我把out.println("GET /myweb/index.html HTTP/1.1");这一行改成了out.println("GET /index.jsp HTTP/1.1");
index.jsp是我从webapps目录下拷贝过来的文件,去除了/myweb,Get请求默认的就是/webapps/index.jsp

运行结果:
忘了贴运行结果了:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 28 Mar 2016 02:59:13 GMT
Connection: close 

原文地址:https://www.cnblogs.com/ysw-go/p/5328171.html