類比Web伺服器

來源:互聯網
上載者:User

標籤:

類比一個web伺服器擷取瀏覽器發來的要求標頭訊息

import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class MyServer {    public static void main(String[] args) throws IOException {        /*         * 建立一個伺服器為了擷取瀏覽器發送的資料         */        System.out.println("伺服器端啟動....");                                ServerSocket ss = new ServerSocket(9090);                Socket s = ss.accept();                System.out.println(s.getInetAddress().getHostAddress()+"....connected");                //接收瀏覽器的資料        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len = in.read(buf);        String str = new String(buf,0,len);                System.out.println(str);                //給瀏覽器一個回饋        PrintWriter out = new PrintWriter(s.getOutputStream(),true);//自動重新整理        out.println("<font size=‘7‘ color = ‘green‘>歡迎光臨,你訪問的是XXX的伺服器</font>");                s.close();        ss.close();                    }}

在瀏覽器的地址欄中輸入:http://127.0.0.1:9090/  後服務端擷取到的要求標頭訊息如下:

GET /myapp/1.jpg HTTP/1.1 //請求行。請求方式 ,請求的資源路徑, http協議版本
要求標頭訊息中的屬性資訊。
Host: 192.168.***.***:9090
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0
Accept-Encoding: gzip, deflate, sdch //封裝的壓縮方式
Accept-Language: zh-CN,zh;q=0.8
空行
//請求體(用空行分割)

類比Web伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.