一、IDHTTP的基本用法

IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快、更节约资源,缺点是需要手动维护cook,连接等

IDHttp的创建,需要引入IDHttp

procedure InitHttp();
begin
    http := TIdHTTP.Create(nil);
    http.ReadTimeout := 30000;
    http.OnRedirect := OnRedirect;
    http.Request.Accept := 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*';
    http.Request.AcceptLanguage := 'zh-cn';
    http.Request.ContentType := 'application/x-www-form-urlencoded';
    http.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)';

http.ProxyParams.ProxyServer := '代理服务器地址';
    http.ProxyParams.ProxyPort := '代理服务器端口';
end;

二、如何取得服务端返回的cookie信息,并添加到http的request对象中

procedure Setcookie;
var
   i: Integer;
   tmp, cookie: String;
begin
   cookie := '';
   for i := 0 to http.Response.RawHeaders.Count - 1 do
   begin
      tmp := http.Response.RawHeaders[i];
      if pos('set-cookie: ', LowerCase(tmp)) = 0 then Continue;
      tmp := Trim(Copy(tmp, Pos('Set-cookie: ', tmp) + Length('Set-cookie: '), Length(tmp)));
      tmp := Trim(Copy(tmp, 0, Pos(';', tmp) - 1));
      if cookie = '' then cookie := tmp else cookie := cookie + '; ' + tmp;
  end;
  if cookie <> '' then
  begin
    for i := 0 to http.Request.RawHeaders.Count - 1 do
    begin
      tmp := http.Request.RawHeaders[i];
      if Pos('cookie', LowerCase(tmp)) = 0 then Continue;
      http.Request.RawHeaders.Delete(i);
      Break;
    end;
    http.Request.RawHeaders.Add('cookie: ' + cookie);
  end;
end;

三、如何取得网页中的所有连接,对代码做修改你也可以实现查找所有图片等等

function GetURLList(Data: String): TStringList;
var
   i: Integer;
   List: TStringList;
   tmp: String;

function Split(Data, Node: String): TStringList;
   var
      Count, i, j: Integer;

function GetFieldCount(Data, Node: String): Integer;
       var
          i: Integer;
       begin
          Result := -1;
          i := Pos(Node, Data);
          if i = 0 then Exit;
             Result := 0;
          while i <> 0 do
          begin
             Inc(Result);
             Delete(Data, 1, i + Length(Node) - 1);
             i := Pos(Node, Data);
          end;
      end;
   begin
      Result := TStringList.Create;
  Count := GetFieldCount(Data, Node);
  for i := 0 to Count - 1 do
  begin
      j := Pos(Node, Data);
      Result.Add(Copy(Data, 1, j - 1));
      Delete(Data, 1, j + Length(Node) - 1);
  end;
  Result.Add(Data);
 end;
begin
 Result := TStringList.Create;
 try
     List := split(Data, 'href=');
     for i := 1 to List.Count - 1 do
     begin
       tmp := List[i];
       tmp := Copy(tmp, 0, Pos('</a>', tmp) - 1);
       tmp := Copy(tmp, 0, Pos('>', tmp) - 1);
       if Pos(' ', tmp) <> 0 then

tmp := Copy(tmp, 0, Pos(' ', tmp) - 1);
       tmp := Q_ReplaceStr(tmp, Char(34), '');
       tmp := Q_ReplaceStr(tmp, Char(39), '');
       if not Compare(CI.Key, tmp) then Continue;
       if Copy(tmp, 1, 7) <> 'http://' then
       begin
         if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []);
         if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []);
         try
           tmp := 'http://' + http.URL.Host + ':' + http.URL.Port + http.URL.Path + tmp;
         except
         end;
       end;
       if Result.IndexOf(tmp) <> -1 then Continue;
          Result.Add(tmp);
     end;
     FreeAndNil(List);
  except

end;
end;

四、如何模拟http的get方法打开一个网页

function GetMethod(http: TIDhttp; URL: String; Max: Integer): String;
var
  RespData: TStringStream;
begin
  RespData := TStringStream.Create('');
  try
    try
      Http.Get(URL, RespData);
      Http.Request.Referer := URL;
      Result := RespData.DataString;
    except
      Dec(Max);
      if Max = 0 then
      begin
        Result := '';
        Exit;
      end;
      Result := GetMethod(http, URL, Max);
    end;
  finally
    FreeAndNil(RespData);
  end;
end;

五、如何模拟http的post方法提交一个网页

function PostMethod(URL, Data: String; max: Integer): String;
var
  PostData, RespData: TStringStream;
begin
  RespData := TStringStream.Create('');
  PostData := TStringStream.Create(Data);
  try
    try
      if http = nil then Exit;
      Http.Post(URL, PostData, RespData);
      Result := RespData.DataString;
      http.Request.Referer := URL;
    except
      Dec(Max);
      if Max = 0 then
      begin
        Result := '';
        Exit;
      end;
      Result := PostMethod(URL, Data, Max);
    end;
  finally
    http.Disconnect;
    FreeAndNil(RespData);
    FreeAndNil(PostData);
  end;
end;

六、伪造session

var
  My_Cookie,tmpcookie:string;

begin
  aIdHttp.Get('http://www.huochepiao.net/');
  tmpcookie:=aIdHttp.Request.CustomHeaders.Values['Set-Cookie'];
  if Pos(';',tmpcookie)>0 then
     My_Cookie:=LeftBStr(tmpcookie,Pos(';',tmpcookie)-1)
  else
     My_Cookie:= tmpcookie;
  //
  aIdHTTP.Request.CustomHeaders.Clear;
  aIdHTTP.Request.CustomHeaders.Add('Cookie:'+My_COOKIE);

end;

IDHttp的基本用法(转)的更多相关文章

  1. Delphi的IDHTTP的基本用法

    一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...

  2. IDHTTP的基本用法

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  3. Delphi IDHTTP用法详解(六种用法)

    一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...

  4. Delphi IDHTTP用法详解

    一.IDHTTP的基本用法  IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等  IDHttp的创建,需要引入 ...

  5. IDHTTP用法详解 good

    一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...

  6. IdHttp 资料

    http://blog.csdn.net/delphizhou/article/details/3085704 IdHttp 资料 网上找了些不过很不好找.今天找了些收藏在一起.以便他人查阅, idh ...

  7. IDHTTP

    Delphi IDHTTP用法详解 一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接 ...

  8. delphi idhttp 实战用法(TIdhttpEx)

    以delphi XE8 自带indy(10.5.8.0)组件为例,分享实战中遇到的问题及解决方法. TIdHttpEx 用法实例01[多线程获取网页](包含完整源码) 实例02(如何Post参数,如何 ...

  9. INDY idhttp Post用法

    http://www.cnblogs.com/tk-del/archive/2013/05/10/3071541.html function Post(AURL: string; ASource: T ...

随机推荐

  1. poj 2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2858   Accepted: 1064 ...

  2. Log4NET简介

    log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具. 前提 最近做项目需要记录系统日志和用 ...

  3. mysql 自旋锁

    自旋(spin)是一种通过不间断地测试来查看一个资源是否变为可用状态的等待操作,用于仅需要等待很短的时间等待所需资源的场景.使用自旋这种“空闲循环(busy-loop)”来完成资源等待的方式要比通过上 ...

  4. python 去掉\n\t多余空格

    >>> import re >>> sss = "SELECT a.id,\n       a.customer_id as user_id,\n     ...

  5. [转] C#中发送消息给指定的窗口,以及接收消息

    原文C#中发送消息给指定的窗口,以及接收消息 public class Note { //声明 API 函数 [DllImport("User32.dll", EntryPoint ...

  6. MorningSale 介绍

    MorningSale是一个WEB端的收集门店销售数据,显示销售数据的简单系统,我相信该系统能够有效的提高销售公司在门店销售数据收集 汇总 分析方面的工作效率. 主要功能介绍如下: 1.查看某个店面 ...

  7. WeChat Official Account Admin Platform Message API Guide

    Keyword: WeChat Message API Text Image Location Link Event Music RichMedia Author: PondBay Studio[We ...

  8. HDU 5607 graph(DP+矩阵乘法)

    [题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...

  9. bzoj 1109 [POI2007]堆积木Klo(LIS)

    [题意] n个数的序列,删除一个数后序列左移,求最后满足i==a[i]的最大个数. [思路] 设最终得到a[i]==i的序列为s,则s应满足: i<j,a[i]<a[j],i-a[i]&l ...

  10. 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?

    UVa11991 Easy Problem from Rujia Liu?  思路:  构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...