本文觸及到Socket TCP/IP編程方面的知識,其實這是很簡單的內容,大家看不明的地方大可以先往下讀,以後再看一遍,可能會覺得很簡單。
很多人寫網站用慣了IIS和Tomcat這些高級的後臺來做網站後臺服務,然而,在這些後臺還沒有問世之前,人們是怎麼編寫網站的呢?他們底層共同遵守的準則和標準又是什麼呢?
這就是http協議和html、js、css標準。這些都是國際統一的標準,和.net、java、php這些語言完全沒有關係的。比例如下面一段代碼:
<html>
<body>
<form runat="server" id="form1">
<%
string str_response="hello world";
........................................
Response.Write(str_response);
%>
<asp:Button ID="bn_1" runat="Server" Text="點擊"/>
</form>
</body>
</html>
長長的一段代碼最終會被解析成下面這樣
<html>
<body>
<form id="form1" action="">
hello world
<input type="submit" value="點擊" name="bn_1"/>
</form>
</body>
</html>
IIS幫助我們,將<%%>裏面的內容轉化成要輸出的內容,同時將<asp:這些標籤,轉化爲相應的標籤(這裏是<input>標籤),這些在html裏面是非標準的標籤,屬於iis規定的標籤,iis有這個責任去將這些標籤轉化爲標準的標籤返回給客戶。
上面這些是基礎,相信很多編寫過網站的人都知道是怎麼一回事,下面回到正題。
下面這段代碼可能大家就很少接觸了:
GET /siteserver/mypage.youlike?h_username=myname HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152;
.NET CLR 3.5.30729)
Host: 127.0.0.1:45632
Content-Length: 93
Connection: Keep-Alive
Cache-Control: no-cache
其實這很簡單,這就是客戶端IE打開一個網址http://127.0.0.1:45632/siteserver/mypage.youlike?h_username=myname,http://127.0.0.1:45632這個網站的後臺收到的全部協議代碼。
這些字符串用換行符相隔。
其中127.0.0.1:45632是一個標準的站點的地址,127.0.0.1是IP,45632是服務端監聽的端口。
User-Agent後面的是瀏覽器信息,GET和HTTP/1.1中間的/siteserver/mypage.youlike 是要訪問的目標地址,當然,假如是自己寫的後臺,這個目標地址其實是一個索引,比如index.aspx,告訴服務器他想要什麼數據,mypage.youlike可能真的存在這個文件,也可能這只是服務端定義的一個索引,服務的swich語句判斷是mypage.youlike,那麼就返回相應的數據。
當然了,/siteserver/mypage.youlike後面緊跟的就是get的參數信息的,相信編寫web的朋友們肯定很清楚。
好了,上面是get的例子,那假如對方是post過來的呢?其實也很簡單,只是後面加了一串東西:
POST /siteserver/mypage.youlike?h_username=myname HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152;
.NET CLR 3.5.30729)
Host: 127.0.0.1:45632
Content-Length: 93
Connection: Keep-Alive
Cache-Control: no-cache
page=12&title_id=300
服務端收到這些數據後,會做什麼操作呢?
其實上面提到的是一個request請求的過程,也就是服務端接收的過程,服務端的另一個過程叫返回,也就是response的響應過程,下面我們要做什麼呢?
我給一個代碼大家參考一下吧:
if(str_request_file=="/siteserver/mypage.youlike")
{
string send_msg = "<!DOCTYPE HTML PUBLIC /" -//W3C//DTD HTML 4.0 Transitional//EN/">";
send_msg += "<html xmlns=/"http://www.w3.org/1999/xhtml/">";
send_msg += "<head><meta http-equiv=/"content-type/" content=/"text/html; charset=gb2312/"/><title>廣業 IIS Server</title></head><body>";
send_msg += "<form action=/"http://127.0.0.1/siteserver/gps.aspx/" method=/"post/" id=/"f1/" name=/"f1/">";
send_msg += "Hello World";
send_msg += "<input id=/"h_my_username/" name=/"h_my_username/" type=/"hidden/" value=/"nl000/" />";
send_msg += "</form>";
send_msg += "<script type=/"text/javascript/">";
//send_msg += "document.getElementById(/"f1/").submit();";
send_msg += "</script>";
///根據page=12&title_id=300,h_username=myname這些參數讀取數據庫,生成一些內容加進send_msg裏
send_msg += "</body>";
send_msg += "</html>";
byte []msg=System.Text.Encoding.GetEncoding("gb2312").GetBytes(send_msg);
string str_senddd="HTTP/1.1 200 OK/r/nVia: 1.1 GY-14SWFMJH7AP2/r/nConnection: Keep-Alive/r/nProxy-Connection: Keep-Alive/r/nContent-Length: "+msg.Length.ToString()+"/r/nContent-Type: text/html/r/nServer: GYXX/1.0/r/nCache-Control: private/r/n/r/n";
sendtoclient(sock, str_senddd + send_msg);
}
sendtoclient這個方法在這裏是向socket寫進str_senddd + send_msg這個字符串
其實最簡單的iis服務就是這樣一個一讀一寫的過程,http的協議離不開這個過程,入門看這個,造化看個人。
聯繫方式:QQ:13896398,電話:13437839547