import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList; /**
* 一个简单的HTTP客户端,发送HTTP请求,模拟浏览器 可打印服务器发送过来的HTTP消息
*/
public class HttpClient {
private static String encoding = "UTF-8"; public static void main(String[] args) {
try {
Socket s = new Socket(InetAddress.getLocalHost(), 8080);
OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append("GET /slsint_gd/reportview/reportViewList.jsp HTTP/1.1\r\n");
sb.append("Host: localhost:8088\r\n");
sb.append("Connection: Keep-Alive\r\n");
// 注,这是关键的关键,忘了这里让我搞了半个小时。这里一定要一个回车换行,表示消息头完,不然服务器会等待
sb.append("\r\n");
osw.write(sb.toString());
osw.flush(); // --输出服务器传回的消息的头信息
InputStream is = s.getInputStream();
String line = null;
int contentLength = 0;// 服务器发送回来的消息长度
// 读取所有服务器发送过来的请求参数头部信息
do {
line = readLine(is, 0);
// 如果有Content-Length消息头时取出
if (line.startsWith("Content-Length")) {
contentLength = Integer.parseInt(line.split(":")[1].trim());
}
// 打印请求部信息
System.out.print(line);
// 如果遇到了一个单独的回车换行,则表示请求头结束
} while (!line.equals("\r\n")); // --输消息的体
System.out.print(readLine(is, contentLength)); // 关闭流
is.close(); } catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /*
* 这里我们自己模拟读取一行,因为如果使用API中的BufferedReader时,它是读取到一个回车换行后
* 才返回,否则如果没有读取,则一直阻塞,直接服务器超时自动关闭为止,如果此时还使用BufferedReader
* 来读时,因为读到最后一行时,最后一行后不会有回车换行符,所以就会等待。如果使用服务器发送回来的
* 消息头里的Content-Length来截取消息体,这样就不会阻塞
*
* contentLe 参数 如果为0时,表示读头,读时我们还是一行一行的返回;如果不为0,表示读消息体,
* 时我们根据消息体的长度来读完消息体后,客户端自动关闭流,这样不用先到服务器超时来关闭。
*/
private static String readLine(InputStream is, int contentLe) throws IOException {
ArrayList lineByteList = new ArrayList();
byte readByte;
int total = 0;
if (contentLe != 0) {
do {
readByte = (byte) is.read();
lineByteList.add(Byte.valueOf(readByte));
total++;
} while (total < contentLe);// 消息体读还未读完
} else {
do {
readByte = (byte) is.read();
lineByteList.add(Byte.valueOf(readByte));
} while (readByte != 10);
} byte[] tmpByteArr = new byte[lineByteList.size()];
for (int i = 0; i < lineByteList.size(); i++) {
tmpByteArr[i] = ((Byte) lineByteList.get(i)).byteValue();
}
lineByteList.clear(); return new String(tmpByteArr, encoding);
}
}

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

Set-Cookie: JSESSIONID=1F27447B2CDF30A69009A219932E259D; Path=/slsint_gd/; HttpOnly

Content-Type: text/html;charset=utf-8

Content-Language: zh-CN

Content-Length: 1881

Date: Mon, 14 Mar 2016 09:11:44 GMT

<html>     <head>         <title>广州农商行外汇数据报送系统</title>         <link rel="stylesheet" type="text/css" media="all" href="/slsint_gd/styles/default.css;jsessionid=1F27447B2CDF30A69009A219932E259D" />         <script type="text/javascript" src="/slsint_gd/scripts/global.js;jsessionid=1F27447B2CDF30A69009A219932E259D"></script>     </head>  <body>   <center>      <div align="center" style=" width:99%">    <div align="left"><span class="pagebanner">共0项</span></div><div align="right"><span class="pagelinks"></span></div> <table id="reports" width="100%" cellpadding="0" class="reportViewList list" cellspacing="0"> <thead> <tr> <th width="20%" class="sortable"></th> <th width="20%" class="sortable"> <a href="http://localhost:8088/slsint_gd/reportview/;jsessionid=1F27447B2CDF30A69009A219932E259D?d-1341553-o=2&amp;d-1341553-s=1">报表名称</a></th> <th width="20%" class="sortable"> <a href="http://localhost:8088/slsint_gd/reportview/;jsessionid=1F27447B2CDF30A69009A219932E259D?d-1341553-o=2&amp;d-1341553-s=2">报表类型</a></th> <th width="20%" class="sortable"> <a href="http://localhost:8088/slsint_gd/reportview/;jsessionid=1F27447B2CDF30A69009A219932E259D?d-1341553-o=2&amp;d-1341553-s=3">机构名称</a></th> <th width="10%" class="sortable"> <a href="http://localhost:8088/slsint_gd/reportview/;jsessionid=1F27447B2CDF30A69009A219932E259D?d-1341553-o=2&amp;d-1341553-s=4">报表日期</a></th> <th width="2%">操作</th></tr></thead> <tbody><tr class="empty"><td colspan="6">没有列表项</td></tr></tbody></table><div align="left"><span class="pagebanner">共0项</span></div><div align="right"><span class="pagelinks"></span></div>     <script type="text/javascript">      <!--       highlightTableRows("reports");      //-->     </script>      </div>      </center> </body> </html>

最新文章

  1. 关于移动开发的一些想法和认识--Android和iOS
  2. 浅谈JS继承
  3. ExtJs 使用点滴 十四 通过设置CheckboxSelectionModel属性值来实现GridPanel复选框可用不可用
  4. 【翻译】Kinect v2程序设计(C++-) AudioBeam篇
  5. dynamic调用时报RuntimeBinderException:“object”未包含“xxx”的定义 错误
  6. spring常量值注入
  7. jQuery 图片随滚动条滚动加载
  8. spring boot 打war包部署,打jar包
  9. Autofac in webapi2
  10. 干货分享!关于APP导航菜单设计你应该了解的一切
  11. BZOJ 3097: Hash Killer I【构造题,思维题】
  12. JDBC查询优化,统计条数
  13. java面试记录
  14. MySQL查看表占用空间大小
  15. 学习笔记——二叉树相关算法的实现(Java语言版)
  16. RT-SA-2019-005 Cisco RV320 Command Injection Retrieval
  17. python大作业
  18. zookeeper选举流程
  19. 关系数据库标准语言SQL——概述
  20. Timeout occurred while waiting for latch: class &#39;ACCESS_METHODS_DATASET_PARENT&#39;

热门文章

  1. 《Head First 设计模式》:装饰者模式
  2. WindowsTerminal折腾记
  3. 一个牛逼的FTP——Wring Ftp
  4. Hexo 踩坑:jquery 报错
  5. vue项目打包踩坑记
  6. Gradle系列之认识Gradle任务
  7. 水题-------判断Digit Generator
  8. jsp课堂笔记1
  9. xctf-pwn level3
  10. linux root 与普通用户之间的切换