使用http協(xié)議和winsockapi實現(xiàn)webzip文件下載
時間:2022-11-17 10:19:00
導(dǎo)語:使用http協(xié)議和winsockapi實現(xiàn)webzip文件下載一文來源于網(wǎng)友上傳,不代表本站觀點,若需要原創(chuàng)文章可咨詢客服老師,歡迎參考。
本方法主要涉及以下四方面知識:html語言、http協(xié)議、winsock編程、多線程程序設(shè)計。
程序實現(xiàn)過程:
1.分析鏈接關(guān)系(限于篇幅,這里只介紹對錨標(biāo)記〈a〉的分析)。
在html中〈a〉標(biāo)記的基本語法為:〈ahref=″...″name=″...″target=″...″〉。其中參數(shù)href的值就是欲獲取的url值。
2.下載。
在http協(xié)議中常用的請求方法有兩種:get和post。本實現(xiàn)使用get方法。最簡化的get請求包如下:
get/index.htmhttp/1.1
“/index.htm”表示客戶端欲下載的文件路徑;“http/1.1”表示協(xié)議版本。
程序生成get請求包,在成功連接對應(yīng)web服務(wù)器的80或其它端口后,使用基于tcp協(xié)議的同步模式套接字發(fā)送請求包并等待返回信息。
服務(wù)器將返回一個應(yīng)答包,大致如下:
http/1.0200ok
...
[數(shù)據(jù)...]
第一行是應(yīng)答信息。如果成功,服務(wù)器將返回“http/1.0200ok”。
第三行是一個空行,用以分隔http包頭和包體(數(shù)據(jù))。
第四行開始就是以字節(jié)流的方式返回的數(shù)據(jù)。
如果使用http,則與上述有兩點不同。
第一,連接時應(yīng)連接服務(wù)器,而不是連接web服務(wù)器。
第二,在生成請求包時,下載文件的url必須寫全url。對上例而言,請求應(yīng)為“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(′網(wǎng)絡(luò)系統(tǒng)未準(zhǔn)備′,′信息′,mb_ok);
wsavernotsupported:
application.messagebox(′未提供網(wǎng)絡(luò)接口′,′信息′,mb_ok);
wsaeinval:
application.messagebox(′網(wǎng)絡(luò)版本不被支持′,′信息′,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//創(chuàng)建本地socket
mysocket:=socket(af_inet,sock_stream,0);
if(mysocket=socket_error)thenbegin
application.messagebox(′初始化失??!′,′信息′,mb_ok);
exit;
end;//生成連接主機(jī)的結(jié)構(gòu)
myclient.sin_family:=af_inet;
myclient.sin_port:=htons(connectedport);
//connectedport:全局變量,記錄連接端口號
strpcopy(recvbuf,getserverip(fileurl));
//getserverip(fileurl):返回服務(wù)器的ip
myclient.sin_addr.s_addr:=inet_addr(recvbuf);//連接服務(wù)器
if(connect(mysocket,myclient,sizeof(myclient))〈〉0)thenbegin
closesocket(mysocket);
exit;
end;//發(fā)請求
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);
//發(fā)送請求讀返回數(shù)據(jù)
reccount:=recv(mysocket,recvbuf,sizeof(recvbuf)-1,0);//判斷是否成功
i:=0;
whilei〈10dobegin
i:=i+1;
//′http/1.0200ok′是成功標(biāo)志
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;
//得到數(shù)據(jù)起始位置
ptemp:=strpos(recvbuf,#13+#10+#13+#10)+4;
index_ch:=ptemp-recvbuf;
//建立下載目錄
tryforcedirectories(extractfilepath(getfillocalpath(fileurl)));
except
end;//創(chuàng)建文件
deletefile(getfillocalpath(fileurl));
myhandle:=filecreate(getfillocalpath(fileurl));//如果未接收完則繼續(xù)
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;//關(guān)閉文件句柄和套接字
fileclose(myhandle);
closesocket(mysocket);
end;