PC版
搜索导航
论文网 > 计算机论文 > 计算机应用论文

使用http协议和winsockapi实现webzip文件下载

本方法主要涉及以下四方面知识:html语言、http协议、winsock编程、多线程程序设计。

  程序实现过程:

  1.分析链接关系(限于篇幅,这里只介绍对锚标记〈a〉的分析)。

  在html中〈a〉标记的基本语法为:〈a href=″...″ name=″...″ target=″...″〉。其中参数href的值就是欲获取的url值。

  2.下载。

  在http协议中常用的请求方法有两种:get和post。本实现使用get方法。最简化的get请求包如下:

  get /index.htm http/1.1

“/index.htm”表示客户端欲下载的文件路径;“http/1.1”表示协议版本。

  程序生成get请求包,在成功连接对应web服务器的80或其它端口后,使用基于tcp协议的同步模式套接字发送请求包并等待返回信息。

  服务器将返回一个应答包,大致如下:



  http/1.0 200 ok

  ...

  [数据...]



  第一行是应答信息。如果成功,服务器将返回“http/1.0 200 ok”。

  第三行是一个空行,用以分隔http包头和包体(数据)。

  第四行开始就是以字节流的方式返回的数据。

  如果使用http代理,则与上述有两点不同。

  第一, 连接时应连接代理服务器,而不是连接web服务器。

  第二,在生成请求包时,下载文件的url必须写全url。对上例而言,请求应为“get http://netsport/index.htm http/1.1”,而不是“get /index.htm http/1.1”。

  具体程序和类(程序使用delphi3.0编制):

  

  1.初始化winsock。

  procedure tform1.formcreate(sender: tobject);

  var

   wversionrequired: word;

   wsdata: twsadata;

  begin

   ismultithread:=true;

  //置″支持多线程″为″真″

   wversionrequired:=makeword(2,0);

   case wsastartup(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

   procedure execute; override;

  public constructor create(url:string);

  end;

  constructor tdownfilethread.create(url:string);

  begin

  fileurl:=url;

  freeonterminate:=true;

  inherited create(false);

  end;

  procedure tdownfilethread.execute;

  var




   mysocket:tsocket; myclient:tsockaddr;

   recvbuf:array [0..332] of char; mycmdstr:string;

   ptemp:pchar;

   myhandle,index_ch,reccount,i:integer;

  begin //创建本地socket

   mysocket:=socket(af_inet,sock_stream,0);

   if (mysocket=socket_error) then begin

   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) then begin

   closesocket(mysocket);

  exit;

   end; //发请求

   if (q_useproxy=0) then

  mycmdstr:=′get ′+extracturlpath(fileurl)+′ http/1.1′

   //extracturlpath(fileurl)返回相对url

   else mycmdstr:=′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;

   while i〈10 do begin

  i:=i+1;

  // ′http/1.0 200 ok′是成功标志

  if ((recvbuf[i]=′ ′) and (recvbuf[i+1]=′2′) and (recvbuf[i+2]=′0′)

   and (recvbuf[i+3]=′0′) and (recvbuf[i+4]=′ ′)) then i:=200;

   end;

   if i〈〉200 then begin closesocket(mysocket); exit; end;

   //得到数据起始位置

   ptemp:=strpos(recvbuf,#13+#10+#13+#10)+4;

   index_ch:=ptemp-recvbuf;

   //建立下载目录

   try forcedirectories(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;