使用http協議和winsockapi實現webzip文件下載
時間:2022-11-17 10:19:00
導語:使用http協議和winsockapi實現webzip文件下載一文來源于網友上傳,不代表本站觀點,若需要原創文章可咨詢客服老師,歡迎參考。
本方法主要涉及以下四方面知識:html語言、http協議、winsock編程、多線程程序設計。
程序實現過程:
1.分析鏈接關系(限于篇幅,這里只介紹對錨標記〈a〉的分析)。
在html中〈a〉標記的基本語法為:〈ahref=″...″name=″...″target=″...″〉。其中參數href的值就是欲獲取的url值。
2.下載。
在http協議中常用的請求方法有兩種:get和post。本實現使用get方法。最簡化的get請求包如下:
get/index.htmhttp/1.1
“/index.htm”表示客戶端欲下載的文件路徑;“http/1.1”表示協議版本。
程序生成get請求包,在成功連接對應web服務器的80或其它端口后,使用基于tcp協議的同步模式套接字發送請求包并等待返回信息。
服務器將返回一個應答包,大致如下:
http/1.0200ok
...
[數據...]
第一行是應答信息。如果成功,服務器將返回“http/1.0200ok”。
第三行是一個空行,用以分隔http包頭和包體(數據)。
第四行開始就是以字節流的方式返回的數據。
如果使用http,則與上述有兩點不同。
第一,連接時應連接服務器,而不是連接web服務器。
第二,在生成請求包時,下載文件的url必須寫全url。對上例而言,請求應為“getnetsport/index.htmhttp/1.1”,而不是“get/index.htmhttp/1.1”。
具體程序和類(程序使用delphi3.0編制):
1.初始化winsock。
proceduretform1.formcreate(sender:tobject);
var
wversionrequired:word;
wsdata:twsadata;
begin
ismultithread:=true;
//置″支持多線程″為″真″
wversionrequired:=makeword(2,0);
casewsastartup(wversionrequired,wsdata)of//初始化winsock
wsasysnotready:
application.messagebox(′網絡系統未準備′,′信息′,mb_ok);
wsavernotsupported:
application.messagebox(′未提供網絡接口′,′信息′,mb_ok);
wsaeinval:
application.messagebox(′網絡版本不被支持′,′信息′,mb_ok);
end;
end;
2.文件下載線程。
tdownfilethread=class(tthread)
private
fileurl:string;
//記錄文件的url
protected
procedureexecute;override;
publicconstructorcreate(url:string);
end;
constructortdownfilethread.create(url:string);
begin
fileurl:=url;
freeonterminate:=true;
inheritedcreate(false);
end;
proceduretdownfilethread.execute;
var
mysocket:tsocket;myclient:tsockaddr;
recvbuf:array[0..332]ofchar;mycmdstr:string;
ptemp:pchar;
myhandle,index_ch,reccount,i:integer;
begin//創建本地socket
mysocket:=socket(af_inet,sock_stream,0);
if(mysocket=socket_error)thenbegin
application.messagebox(′初始化失??!′,′信息′,mb_ok);
exit;
end;//生成連接主機的結構
myclient.sin_family:=af_inet;
myclient.sin_port:=htons(connectedport);
//connectedport:全局變量,記錄連接端口號
strpcopy(recvbuf,getserverip(fileurl));
//getserverip(fileurl):返回服務器的ip
myclient.sin_addr.s_addr:=inet_addr(recvbuf);//連接服務器
if(connect(mysocket,myclient,sizeof(myclient))〈〉0)thenbegin
closesocket(mysocket);
exit;
end;//發請求
if(q_useproxy=0)then
mycmdstr:=′get′+extracturlpath(fileurl)+′http/1.1′
//extracturlpath(fileurl)返回相對url
elsemycmdstr:=′get′+fileurl+′http/1.1′;//使用寫全url
strpcopy(recvbuf,mycmdstr);
i:=length(mycmdstr);
recvbuf[i]:=#13;inc(i);recvbuf[i]:=#10;inc(i);
recvbuf[i]:=#13;inc(i);recvbuf[i]:=#10;inc(i);
recvbuf[i]:=#0;
send(mysocket,recvbuf,i,0);
//發送請求讀返回數據
reccount:=recv(mysocket,recvbuf,sizeof(recvbuf)-1,0);//判斷是否成功
i:=0;
whilei〈10dobegin
i:=i+1;
//′http/1.0200ok′是成功標志
if((recvbuf[i]=′′)and(recvbuf[i+1]=′2′)and(recvbuf[i+2]=′0′)
and(recvbuf[i+3]=′0′)and(recvbuf[i+4]=′′))theni:=200;
end;
ifi〈〉200thenbeginclosesocket(mysocket);exit;end;
//得到數據起始位置
ptemp:=strpos(recvbuf,#13+#10+#13+#10)+4;
index_ch:=ptemp-recvbuf;
//建立下載目錄
tryforcedirectories(extractfilepath(getfillocalpath(fileurl)));
except
end;//創建文件
deletefile(getfillocalpath(fileurl));
myhandle:=filecreate(getfillocalpath(fileurl));//如果未接收完則繼續
while(reccount〈〉0)do
begin
filewrite(myhandle,recvbuf[index_ch],reccount-(index_ch));
index_ch:=0;
reccount:=recv(mysocket,recvbuf,sizeof(recvbuf)-1,0);
end;//關閉文件句柄和套接字
fileclose(myhandle);
closesocket(mysocket);
end;