背景:因为想写一个测试tomcat连接的程序,就试试自已去构造Http请求。找了一下发现Apache下有一个比较成熟的轮子HttpComponets.
使用库:Apache Http Client
HttpClient是一个代码级的Http客户端工具,可以使用它模拟浏览器向Http服务器发送请求。
其中HttpClient是HttpComponents项目其中的一部份,HttpComponents简称为hc.
见http://hc.apache.org/
使用HttpClient还需要HttpCore.这里其中包括Http请求与Http响应的代码封装。
现在导入两个jar包:
httpclient-4.0.jar
httpcore-4.0.jar
httpmime-4.0.jar
开始一个请求代码如下:
- HttpClient httpClient = new DefaultHttpClient();
- //构造请求,如果想使用POST则new一个HttpPost
- HttpGet get = new HttpGet("http://www.iteye.com");
- HttpResponse response = httpClient.execute(get);
- //先从响应头得到实体
- HttpEntity entity = response.getEntity();
- //得到实体输入流
- InputStream inSm = entity.getContent();
- Scanner inScn = new Scanner(inSm);
- while(inScn.hasNextLine()){
- System.out.println(inScn.nextLine());
- }
- entity.consumeContent();
HttpClient httpClient = new DefaultHttpClient(); //构造请求,如果想使用POST则new一个HttpPost HttpGet get = new HttpGet("http://www.iteye.com"); HttpResponse response = httpClient.execute(get); //先从响应头得到实体 HttpEntity entity = response.getEntity(); //得到实体输入流 InputStream inSm = entity.getContent(); Scanner inScn = new Scanner(inSm); while(inScn.hasNextLine()){ System.out.println(inScn.nextLine()); } entity.consumeContent();
这个是在HC例子中有代码作了少量的修改。
输出:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <title>JavaEye Java编程 Spring框架 AJAX技术 Agile敏捷软件开发 ruby on rails实践 - JavaEye做最棒的软件开发交流社区</title>
- <meta name="description" content="Java编程,Spring Struts Webwork框架深入,XMLHTTP AJAX开发,Java Web开发,Java企业应用,Java设计模式,Java开源框架,Java应用服务器,Rich Client讨论,Ruby编程,Ruby DSL开发,Ruby on rails实践,JavaScript编程,敏捷软件开发XP TDD,软件配置管理,软件测试,项目管理UML,数据库,C#/.net C/C++ Erlang/FP PHP/Linux平台,精通Hibernate" />
- <meta name="keywords" content="Java编程 Spring框架 AJAX技术 Agile敏捷软件开发 ruby on rails实践 JavaEye深度技术社区" />
- <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon" />
- <link href="/rss" rel="alternate" title="JavaEye技术网站RSS频道订阅" type="application/rss+xml" />
- <link href="http://www.iteye.com/stylesheets/homepage.css?1263389049" media="screen" rel="stylesheet" type="text/css" />
-
- </head>
-
- ...省略,证明以上代码已经成功
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>JavaEye Java编程 Spring框架 AJAX技术 Agile敏捷软件开发 ruby on rails实践 - JavaEye做最棒的软件开发交流社区</title> <meta name="description" content="Java编程,Spring Struts Webwork框架深入,XMLHTTP AJAX开发,Java Web开发,Java企业应用,Java设计模式,Java开源框架,Java应用服务器,Rich Client讨论,Ruby编程,Ruby DSL开发,Ruby on rails实践,JavaScript编程,敏捷软件开发XP TDD,软件配置管理,软件测试,项目管理UML,数据库,C#/.net C/C++ Erlang/FP PHP/Linux平台,精通Hibernate" /> <meta name="keywords" content="Java编程 Spring框架 AJAX技术 Agile敏捷软件开发 ruby on rails实践 JavaEye深度技术社区" /> <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon" /> <link href="/rss" rel="alternate" title="JavaEye技术网站RSS频道订阅" type="application/rss+xml" /> <link href="http://www.iteye.com/stylesheets/homepage.css?1263389049" media="screen" rel="stylesheet" type="text/css" /> </head> ...省略,证明以上代码已经成功
当然这个只是大刀一小试。现在还离我的目标差一点点:
需要做个头,我还不太清楚有什么头可以使用,那样我又写了个程序看看IE的头到底是怎样子的,
没有抓包程序,只好自已写个程序:
- ServerSocket socketServer = new ServerSocket(10086);
- Socket client = socketServer.accept();
- InputStream inClient = client.getInputStream();
- Scanner inScn = new Scanner(inClient);
- while(inScn.hasNextLine()){
- System.out.println(inScn.nextLine());
- }
- inScn.close();
- client.close();
- socketServer.close();
ServerSocket socketServer = new ServerSocket(10086); Socket client = socketServer.accept(); InputStream inClient = client.getInputStream(); Scanner inScn = new Scanner(inClient); while(inScn.hasNextLine()){ System.out.println(inScn.nextLine()); } inScn.close(); client.close(); socketServer.close();
在IE地址栏上输入:
http://locahost:10086/
点确定后可以得到程序输出:
- GET / HTTP/1.1
- Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
- Accept-Language: zh-CN
- User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 87; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
- Accept-Encoding: gzip, deflate
- Host: localhost:10086
- Connection: Keep-Alive
GET / HTTP/1.1 Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */* Accept-Language: zh-CN User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 87; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0) Accept-Encoding: gzip, deflate Host: localhost:10086 Connection: Keep-Alive
分析请求包中这六个头信息。可以参考以下文章:
http://zsxxsz.iteye.com/blog/568250
现在我使用HC模拟IE的请求,修改后代码如下:
- HttpClient httpClient = new DefaultHttpClient();
- HttpGet get = new HttpGet("http://www.iteye.com");
-
- get.setHeader("Accept", "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*");
- get.setHeader("Accept-Language","zh-CN");
- get.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 87; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)");
- get.setHeader("Accept-Encoding","gzip, deflate");
- get.setHeader("Host","you never be know");
- get.setHeader("Connection","Keep-Alive");
-
- HttpResponse response = httpClient.execute(get);