Chinaunix首页 | 论坛 | 博客
  • 博客访问: 411179
  • 博文数量: 54
  • 博客积分: 1186
  • 博客等级: 少尉
  • 技术积分: 668
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 04:57
文章分类

全部博文(54)

文章存档

2013年(1)

2012年(6)

2011年(47)

我的朋友

分类: BSD

2011-04-30 19:27:04

在Delphi中无法直接使用LibCurl的最新版本,在网上只查到有个CurlPas的调用接口,但已经6年没更新了。

为了解决这个问题,我只有在VC中写LibCurl的实现代码,然后在Delphi调用VC的DLL。


在这个压缩包中只有一个http.dll,是在VC中实现Http的Get/Head/Post,其实还包括2个下载文件函数,其中一个下载函数是将文件的数据保存到IStream流中的,但在Delphi中不知道怎么写调用参数,所以就没用上。

有了调用接口,就写个简单的类来调用吧!

  1. unit http;

  2. interface

  3. uses
  4.     Windows,
  5.     SysUtils,
  6.     Classes;

  7. type
  8.     TWinHttp = class
  9.     private
  10.         procedure Initializing;
  11.         procedure HttpInfo;
  12.         
  13.     protected
  14.         FUrl,FCookie,FSavePath,FReferer,FProxy,FHead,FSource,FPostData,
  15.         FLocation:string;
  16.         FStateCode,FProxyPort:Integer;

  17.     public
  18.         constructor Create;overload;
  19.         function Get:string;
  20.         function Head:string;
  21.         function Post:string;
  22.         function StateCode:Integer;
  23.         
  24.     published
  25.         property Url:string read FUrl write FUrl;
  26.         property Cookie:string read FCookie write FCookie;
  27.         property SavePath:string read FSavePath write FSavePath;
  28.         property PostData:string read FPostData write FPostData;
  29.         property Referer:string read FReferer write FReferer;
  30.         property Proxy:string read FProxy write FProxy;
  31.         property ProxyPort:Integer read FProxyPort write FProxyPort;
  32.         property Location:string read FLocation write FLocation;


  33.     end;



  34. implementation

  35. const BUFSIZ = 204800;

  36. //==============================================================================
  37. // 静态调用http.dll
  38. //==============================================================================


  39. //下载文件
  40. function GetFile( Url:PChar; Cookie:PChar; SavePath:PChar ):Boolean;
  41.     cdecl external 'http.dll' name 'GetFile';

  42. //获取网页源码
  43. function GetUrl( Url:PChar; Referer:PChar; Cookie:PChar;
  44.     Proxy:PChar; ProxyPort:Integer;
  45.     Source:PChar; nSrc:Integer ):Boolean;
  46.     cdecl external 'http.dll' name 'GetUrl';

  47. //获取网页HTTP头
  48. function GetHead( Url:PChar; Referer:PChar; Cookie:PChar;
  49.     Proxy:PChar; ProxyPort:Integer;
  50.     Source:PChar; nSrc:Integer ):Boolean;
  51.     cdecl external 'http.dll' name 'GetHead';

  52. //HTTP提交
  53. function PostUrl( Url:PChar; Referer:PChar; Cookie:PChar;
  54.      PostData:PChar; Proxy:PChar; ProxyPort:Integer;
  55.     Source:PChar; nSrc:Integer ):Boolean;
  56.     cdecl external 'http.dll' name 'PostUrl';



  57. //==============================================================================
  58. // TWinHttp
  59. //==============================================================================

  60. constructor TWinHttp.Create;
  61. begin
  62.     Initializing;
  63. end;

  64. procedure TWinHttp.Initializing;
  65. begin
  66.     FUrl := '';
  67.     FCookie := '';
  68.     FSavePath := '';
  69.     FReferer := '';
  70.     FProxy := '';
  71.     FHead := '';
  72.     FSource := '';
  73.     FPostData := '';
  74.     FStateCode := 0;
  75.     FProxyPort := 0;
  76. end;

  77. procedure TWinHttp.HttpInfo;
  78. var
  79.     i,nPos,nIndex:Integer;
  80.     CookieList:TStringList;
  81. begin
  82.     CookieList := TStringList.Create;
  83.     
  84.     //提取HTTP头
  85.     nPos := Pos( #13#10#13#10, FSource );
  86.     if nPos <> 0 then
  87.     begin
  88.         FHead := Copy( FSource, 1, nPos );

  89.         //提取状态码
  90.         FStateCode := StrToInt( Copy( FHead, 10, 3 ) );

  91.         //Location
  92.         if FStateCode = 302 then
  93.         begin
  94.             nPos := Pos( 'Location: ', FHead );

  95.             FLocation := Copy( FHead, nPos + Length( 'Location: ' ),
  96.                 Length( FHead ) - nPos );
  97.             FLocation := Copy( FLocation, 1, Pos( #13#10, FLocation ) - 1 );
  98.         end;

  99.         //网页内容
  100.         FSource := Copy( FSource, nPos + Length( #13#10#13#10 ),
  101.             Length( FSource ) - nPos );

  102.         if ( Pos( 'charset=utf-8', FSource ) <> 0 ) or
  103.             ( Pos( 'charset=UTF-8', FSource ) <> 0 ) then
  104.         begin
  105.             FSource := Utf8ToAnsi( FSource );
  106.         end;

  107.     end;

  108.     //提取Cookie
  109.     ExtractStrings( [#13], [], PChar( FHead ), CookieList );
  110.     if CookieList.Count > 0 then
  111.     begin
  112.         FCookie := '';
  113.         for i := 0 to CookieList.Count - 1 do
  114.         begin
  115.             nPos := Pos( 'Set-Cookie: ', CookieList[i] );
  116.             if nPos = 1 then
  117.             begin
  118.                 FCookie := FCookie +
  119.                     Copy( CookieList[i], Length( 'Set-Cookie: ' ) + 1,
  120.                     Length( CookieList[i] ) - Length( 'Set-Cookie: ' ) + 1 );
  121.                 nIndex := Length( FCookie );
  122.                 if FCookie[nIndex] <> ';' then
  123.                     FCookie := FCookie + '; ';
  124.             end;
  125.         end;
  126.     end;

  127.     CookieList.Free;
  128. end;

  129. function TWinHttp.Get:string;
  130. var
  131.     pSource:PChar;
  132. begin
  133.     FSource := '';
  134.     FHead := '';
  135.     FLocation := '';
  136.     FStateCode := 0;

  137.     
  138.     if FUrl <> '' then
  139.     begin
  140.         GetMem( pSource, BUFSIZ );
  141.         ZeroMemory( pSource, BUFSIZ );

  142.         if GetUrl( PChar( FUrl ), PChar( FReferer ), PChar( FCookie ),
  143.             PChar( FProxy ), FProxyPort, pSource, BUFSIZ ) then
  144.         begin
  145.             FSource := StrPas( pSource );
  146.         end;
  147.         FreeMem( pSource );

  148.         //提取Http头中的各项数据
  149.         HttpInfo;
  150.     end;

  151.     Result := FSource;
  152. end;

  153. function TWinHttp.Head:string;
  154. var
  155.     pSource:PChar;
  156. begin
  157.     FSource := '';
  158.     FLocation := '';
  159.     FHead := '';
  160.     FStateCode := 0;

  161.     if FUrl <> '' then
  162.     begin
  163.         GetMem( pSource, BUFSIZ );
  164.         ZeroMemory( pSource, BUFSIZ );

  165.         if GetHead( PChar( FUrl ), PChar( FReferer ), PChar( FCookie ),
  166.             PChar( FProxy ), FProxyPort, pSource, BUFSIZ ) then
  167.         begin
  168.             FSource := StrPas( pSource );
  169.         end;
  170.         FreeMem( pSource );

  171.         FHead := FSource;

  172.         //提取Http头中的各项数据
  173.         HttpInfo;
  174.     end;

  175.     Result := FHead;
  176. end;

  177. function TWinHttp.Post:string;
  178. var
  179.     pSource:PChar;
  180. begin
  181.     FSource := '';
  182.     FLocation := '';
  183.     FHead := '';
  184.     FStateCode := 0;

  185.     if ( FUrl <> '' ) and ( FPostData <> '' ) then
  186.     begin
  187.         GetMem( pSource, BUFSIZ );
  188.         ZeroMemory( pSource, BUFSIZ );

  189.         if PostUrl( PChar( FUrl ), PChar( FReferer ),
  190.              PChar( FPostData ), PChar( FCookie ), 
  191.              PChar( FProxy ), FProxyPort,
  192.              pSource, BUFSIZ ) then
  193.         begin
  194.             FSource := StrPas( pSource );
  195.         end;
  196.         FreeMem( pSource );

  197.         //提取Http头中的各项数据
  198.         HttpInfo;
  199.     end;
  200. end;

  201. function TWinHttp.StateCode:Integer;
  202. begin
  203.     Result := FStateCode;
  204. end;

  205. end.
阅读(5218) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~