1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.io.OutputStreamWriter; 4 import java.net.InetAddress; 5 import java.net.Socket; 6 import java.net.UnknownHostException; 7 import java.util.ArrayList; 8 9 /** 10 * 一個簡單的HTTP客戶端,發送HTTP請求,模擬瀏覽器 可打印服務器發送過來的HTTP消息 11 */ 12 public class HttpClient { 13 private static String encoding = "UTF-8"; 14 15 public static void main(String[] args) { 16 try { 17 Socket s = new Socket(InetAddress.getLocalHost(), 8080); 18 OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream()); 19 StringBuffer sb = new StringBuffer(); 20 sb.append("GET /slsint_gd/reportview/reportViewList.jsp HTTP/1.1\r\n"); 21 sb.append("Host: localhost:8088\r\n"); 22 sb.append("Connection: Keep-Alive\r\n"); 23 // 注,這是關鍵的關鍵,忘了這裏讓我搞了半個小時。這裏必定要一個回車換行,表示消息頭完,否則服務器會等待 24 sb.append("\r\n"); 25 osw.write(sb.toString()); 26 osw.flush(); 27 28 // --輸出服務器傳回的消息的頭信息 29 InputStream is = s.getInputStream(); 30 String line = null; 31 int contentLength = 0;// 服務器發送回來的消息長度 32 // 讀取全部服務器發送過來的請求參數頭部信息 33 do { 34 line = readLine(is, 0); 35 // 若是有Content-Length消息頭時取出 36 if (line.startsWith("Content-Length")) { 37 contentLength = Integer.parseInt(line.split(":")[1].trim()); 38 } 39 // 打印請求部信息 40 System.out.print(line); 41 // 若是遇到了一個單獨的回車換行,則表示請求頭結束 42 } while (!line.equals("\r\n")); 43 44 // --輸消息的體 45 System.out.print(readLine(is, contentLength)); 46 47 // 關閉流 48 is.close(); 49 50 } catch (UnknownHostException e) { 51 e.printStackTrace(); 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 } 56 57 /* 58 * 這裏咱們本身模擬讀取一行,由於若是使用API中的BufferedReader時,它是讀取到一個回車換行後 59 * 才返回,不然若是沒有讀取,則一直阻塞,直接服務器超時自動關閉爲止,若是此時還使用BufferedReader 60 * 來讀時,由於讀到最後一行時,最後一行後不會有回車換行符,因此就會等待。若是使用服務器發送回來的 61 * 消息頭裏的Content-Length來截取消息體,這樣就不會阻塞 62 * 63 * contentLe 參數 若是爲0時,表示讀頭,讀時咱們仍是一行一行的返回;若是不爲0,表示讀消息體, 64 * 時咱們根據消息體的長度來讀完消息體後,客戶端自動關閉流,這樣不用先到服務器超時來關閉。 65 */ 66 private static String readLine(InputStream is, int contentLe) throws IOException { 67 ArrayList lineByteList = new ArrayList(); 68 byte readByte; 69 int total = 0; 70 if (contentLe != 0) { 71 do { 72 readByte = (byte) is.read(); 73 lineByteList.add(Byte.valueOf(readByte)); 74 total++; 75 } while (total < contentLe);// 消息體讀還未讀完 76 } else { 77 do { 78 readByte = (byte) is.read(); 79 lineByteList.add(Byte.valueOf(readByte)); 80 } while (readByte != 10); 81 } 82 83 byte[] tmpByteArr = new byte[lineByteList.size()]; 84 for (int i = 0; i < lineByteList.size(); i++) { 85 tmpByteArr[i] = ((Byte) lineByteList.get(i)).byteValue(); 86 } 87 lineByteList.clear(); 88 89 return new String(tmpByteArr, encoding); 90 } 91 }
HTTP/1.1 200 OK javascript
Server: Apache-Coyote/1.1 css
Set-Cookie: JSESSIONID=1F27447B2CDF30A69009A219932E259D; Path=/slsint_gd/; HttpOnly html
Content-Type: text/html;charset=utf-8 java
Content-Language: zh-CN 瀏覽器
Content-Length: 1881 服務器
Date: Mon, 14 Mar 2016 09:11:44 GMTsession
<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&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&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&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&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>app