package cn.xxy.net.p7.ie_server;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyBrowser {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException,IOException {
Socket s = new Socket("127.0.0.1", 80);
// 模拟浏览器,给tomcat服务端发送符合http协议的请求消息
PrintWriter out = new PrintWriter(s.getOutputStream());
out.println("GET /myweb/1.html HTTP/1.1");
out.println("Host: 127.0.0.1:80");
out.println("Connection: close");
out.println("Accept: */*");
out.println("Accept-Language: zh-CN");
out.println("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; SE 2.X MetaSr 1.0)");
out.println("Accept-Encoding: gzip, deflate");
out.println();
out.println() ;
InputStream in = s.getInputStream();
byte b[] = new byte[1024];
int leng = 0;
while ((leng = in.read(b)) != -1) {
String str = new String(b, 0, leng);
System.out.println(str);
}
s.close();
}
}
程序运行之后 tomcat应该会给我发一个回应信息,调试的时候发现tomcat没有数据返回,网页访问的时候是正常的。是不是我的请求没有写对,请高手帮忙讲解下,谢谢了!!
Tomcat 浏览器
------解决方案--------------------
out.println("GET /test/aa.jsp HTTP/1.1");
out.println("Host: 127.0.0.1");//这里就不用端口号了
out.println("Connection: close");
out.println("Accept: */*");
out.println("Accept-Language: zh-CN");
out.println("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; SE 2.X MetaSr 1.0)");
out.println("Accept-Encoding: gzip, deflate");
out.println();
out.println() ;
out.flush();//这里flush一下