zoukankan      html  css  js  c++  java
  • IDHTTP用法详解 good

    1. 一、IDHTTP的基本用法  
    2.   
    3. IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快、更节约资源,缺点是需要手动维护cook,连接等  
    4.   
    5. IDHttp的创建,需要引入IDHttp  
    6.   
    7. procedure InitHttp();  
    8. begin  
    9.     http := TIdHTTP.Create(nil);  
    10.     http.ReadTimeout := 30000;  
    11.     http.OnRedirect := OnRedirect;  
    12.     http.Request.Accept := 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*';  
    13.     http.Request.AcceptLanguage := 'zh-cn';  
    14.     http.Request.ContentType := 'application/x-www-form-urlencoded';  
    15.     http.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)';  
    16.   
    17.     http.ProxyParams.ProxyServer := '代理服务器地址';  
    18.     http.ProxyParams.ProxyPort := '代理服务器端口';  
    19. end;  
    20.   
    21.   
    22. 二、如何取得服务端返回的cookie信息,并添加到http的request对象中  
    23.   
    24.   
    25. procedure Setcookie;  
    26. var  
    27.    i: Integer;  
    28.    tmp, cookie: String;  
    29. begin  
    30.    cookie := '';  
    31.    for i := to http.Response.RawHeaders.Count - do  
    32.    begin  
    33.        tmp := http.Response.RawHeaders[i];  
    34.       if pos('set-cookie: ', LowerCase(tmp)) = then Continue;  
    35.     tmp := Trim(Copy(tmp, Pos('Set-cookie: ', tmp) + Length('Set-cookie: '), Length(tmp)));  
    36.     tmp := Trim(Copy(tmp, 0, Pos(';', tmp) - 1));  
    37.       if cookie = '' then cookie := tmp else cookie := cookie + '; ' + tmp;  
    38. end;  
    39. if cookie <> '' then  
    40. begin  
    41.     for i := to http.Request.RawHeaders.Count - do  
    42.     begin  
    43.       tmp := http.Request.RawHeaders[i];  
    44.       if Pos('cookie', LowerCase(tmp)) = then Continue;  
    45.       http.Request.RawHeaders.Delete(i);  
    46.       Break;  
    47.     end;  
    48.     http.Request.RawHeaders.Add('cookie: ' + cookie);  
    49. end;  
    50. end;  
    51.   
    52.   
    53. 三、如何取得网页中的所有连接,对代码做修改你也可以实现查找所有图片等等  
    54.   
    55.   
    56. function GetURLList(Data: String): TStringList;  
    57. var  
    58. i: Integer;  
    59. List: TStringList;  
    60. tmp: String;  
    61.   
    62.   
    63. function Split(Data, Node: String): TStringList;  
    64.    var  
    65.     Count, i, j: Integer;  
    66.        
    67.   
    68.        function GetFieldCount(Data, Node: String): Integer;  
    69.       var  
    70.          i: Integer;  
    71.       begin  
    72.           Result := -1;  
    73.           i := Pos(Node, Data);  
    74.          if i = then Exit;  
    75.              Result := 0;  
    76.           while i <> do  
    77.           begin  
    78.             Inc(Result);  
    79.              Delete(Data, 1, i + Length(Node) - 1);  
    80.             i := Pos(Node, Data);  
    81.           end;  
    82.     end;  
    83.    begin  
    84.       Result := TStringList.Create;  
    85.   Count := GetFieldCount(Data, Node);  
    86.   for i := to Count - do  
    87.   begin  
    88.       j := Pos(Node, Data);  
    89.       Result.Add(Copy(Data, 1, j - 1));  
    90.       Delete(Data, 1, j + Length(Node) - 1);  
    91.   end;  
    92.   Result.Add(Data);  
    93.  end;  
    94. begin  
    95.  Result := TStringList.Create;  
    96.  try  
    97.     List := split(Data, 'href=');  
    98.      for i := to List.Count - do  
    99.      begin  
    100.       tmp := List[i];  
    101.        tmp := Copy(tmp, 0, Pos('</a>', tmp) - 1);  
    102.        tmp := Copy(tmp, 0, Pos('>', tmp) - 1);  
    103.        if Pos(' ', tmp) <> then  
    104.   
    105.           tmp := Copy(tmp, 0, Pos(' ', tmp) - 1);  
    106.        tmp := Q_ReplaceStr(tmp, Char(34), '');  
    107.      tmp := Q_ReplaceStr(tmp, Char(39), '');  
    108.        if not Compare(CI.Key, tmp) then Continue;  
    109.        if Copy(tmp, 1, 7) <> 'http://' then  
    110.      begin  
    111.          if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []);  
    112.        if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []);  
    113.         try  
    114.          tmp := 'http://' + http.URL.Host + ':' + http.URL.Port + http.URL.Path + tmp;  
    115.         except  
    116.          end;  
    117.        end;  
    118.        if Result.IndexOf(tmp) <> -then Continue;  
    119.           Result.Add(tmp);  
    120.      end;  
    121.    FreeAndNil(List);  
    122. except  
    123.   
    124. end;  
    125. end;  
    126.   
    127.   
    128. 四、如何模拟http的get方法打开一个网页  
    129.   
    130.   
    131. function GetMethod(http: TIDhttp; URL: String; Max: Integer): String;  
    132. var  
    133. RespData: TStringStream;  
    134. begin  
    135. RespData := TStringStream.Create('');  
    136. try  
    137.     try  
    138.       Http.Get(URL, RespData);  
    139.       Http.Request.Referer := URL;  
    140.       Result := RespData.DataString;  
    141.     except  
    142.       Dec(Max);  
    143.       if Max = then  
    144.       begin  
    145.         Result := '';  
    146.         Exit;  
    147.       end;  
    148.       Result := GetMethod(http, URL, Max);  
    149.     end;  
    150. finally  
    151.     FreeAndNil(RespData);  
    152. end;  
    153. end;  
    154.   
    155.   
    156. 五、如何模拟http的post方法提交一个网页  
    157.   
    158.   
    159. function PostMethod(URL, Data: String; max: Integer): String;  
    160. var  
    161. PostData, RespData: TStringStream;  
    162. begin  
    163. RespData := TStringStream.Create('');  
    164. PostData := TStringStream.Create(Data);  
    165. try  
    166.     try  
    167.       if http = nil then Exit;  
    168.       Http.Post(URL, PostData, RespData);  
    169.       Result := RespData.DataString;  
    170.       http.Request.Referer := URL;  
    171.     except  
    172.       Dec(Max);  
    173.       if Max = then  
    174.       begin  
    175.         Result := '';  
    176.         Exit;  
    177.       end;  
    178.       Result := PostMethod(URL, Data, Max);  
    179.     end;  
    180. finally  
    181.     http.Disconnect;  
    182.     FreeAndNil(RespData);  
    183.     FreeAndNil(PostData);  
    184. end;  
    185. end;  
    186.   
    187.   
    188. 六、伪造session  
    189.   
    190. var  
    191. My_Cookie,tmpcookie:string;  
    192.   
    193. begin  
    194. aIdHttp.Get('http://www.huochepiao.net/');  
    195. tmpcookie:=aIdHttp.Request.CustomHeaders.Values['Set-Cookie'];  
    196.    if Pos(';',tmpcookie)>then  
    197.      My_Cookie:=LeftBStr(tmpcookie,Pos(';',tmpcookie)-1)  
    198. else  
    199.      My_Cookie:= tmpcookie;  
    200. //  
    201. aIdHTTP.Request.CustomHeaders.Clear;  
    202. aIdHTTP.Request.CustomHeaders.Add('Cookie:'+My_COOKIE);  
    203.   
    204. end;  
     

    http://blog.csdn.net/s371795639/article/details/53634601

  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 连接字符串
    大多云盘都挤兑在了企业级市场
    115能做到100个亿(2016年05月31日),2013营收两亿元
    Dropbox 有哪些鲜为人知的使用技巧?
    Qt 添加外部库文件(四种方法)
    百度全新的ARM架构服务器,一个2U机箱装6台,每台4个3T硬盘,每个机箱共72TB
    EntityFramework中支持BulkInsert扩展
    基于A2DFramework的事件机制实现
    事件机制与消息机制的架构设计区别
    .net平台下socket异步通讯(代码实例)
  • 原文地址:https://www.cnblogs.com/findumars/p/6323719.html
Copyright © 2011-2022 走看看