Delphi IDHTTP用法详解

一、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的get 和 post的使用

function Tform1.IdHttpInit;
begin
idhtp1.AllowCookies:=True;
idhtp1.HTTPOptions:=[hoForceEncodeParams];
idhtp1.ProtocolVersion:=pv1_;
idhtp1.Request.ContentType:='Content-Type=application/x-www-form-urlencoded; charset=UTF-8';
idhtp1.Request.CacheControl:='no-cache';
idhtp1.Request.UserAgent:='User-Agent=Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)';
idhtp1.Request.Accept:='Accept=*/*';
idhtp1.Request.AcceptEncoding:='Accept-Encoding=gzip,deflate';
idhtp1.Request.AcceptCharSet:='Accept-Charset=gb2312,utf-8;q=0.7,*;q=0.7';
idhtp1.Request.Connection:='Connection=keep-alive';
idhtp1.Request.Host:='Host=kyfw.12306.cn';
idhtp1.Request.Referer:='https://kyfw.12306.cn/otn/login/init';
idhtp1.Request.AcceptLanguage:='Accept-Language=zh-cn';
Result:='';
end; function Tform1.GetCodeImg;
var url:string;
response:TMemoryStream;
jpg:TJPEGImage;
begin
response:=TMemoryStream.Create;
url:='https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&0.3110086616057921';
idhtp1.Get(url,response);
jpg:=TJPEGImage.Create;
jpg.LoadFromStream(response);
Result:='';
end; function Tform1.Login;
var
url : string;
Params:TStrings;
begin
Params:=TStringList.Create;
Params.Add('loginUserDTO.user_name=xxxxxx@qq.com');//用户名
Params.Add('userDTO.password=xxxxxxx');//密码
Params.Add('randCode=ku6a');//验证码
Params.Add('randCode_validate=');
Params.Add('OTcyNTU4=ZjIzYzc3MWJmMDk4OTk5YQ==');
Params.Add('myversion=undefined');
end;

IDHTTP的更多相关文章

  1. 学习indy组件之一idhttp的使用方法

    登录 注册 百度首页 新闻 网页 贴吧 知道 音乐 图片 视频 地图 百科 文库 经验 搜索答案我要提问 首页 分类 公社 知道行家 问医生 高质量问答 经验 个人中心手机知道开放平台   关于del ...

  2. [delphi]indy idhttp post方法

    网易 博客 LOFTCam-用心创造滤镜 LOFTER-最美图片社交APP 送20张免费照片冲印 > 注册登录  加关注 techiepc的博客 万事如意 首页 日志 LOFTER 相册 音乐 ...

  3. delphi请求idhttp数据

    idhttp ss : TStringStream; begin ss := TStringStream.)); { 指定gb2312的中文代码页,或者54936(gb18030)更好些 utf8 对 ...

  4. INDY idhttp Post用法

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

  5. IDHttp的基本用法(转)

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

  6. Delphi Idhttp Post提交 Aspx/Asp.net 时 500错误的解决办法。

    一直使用Delphi写程序,因为习惯了,用起来方便. 但是有一个问题困扰了我半年了.就是使用Idhttp Post提交时候总会有莫名其妙的错误,大部分网站没问题,但是一遇到Asp.net就报错500. ...

  7. idhttp.post方式 调用datasnap rest 远程方法

    idhttp.get方式调用,这种比较简单,大家都会.post方式网上却没有任何成功的代码,本人也是摸索了一个上午才搞定. 分享给大家. (1)post方式调用的远程方法,方法名必须加“update” ...

  8. idHTTP最简洁的修改和取得Cookie例子

    procedure TForm1.Button1Click(Sender: TObject); var HTTP: TidHTTP; html, s: string; i: integer; begi ...

  9. idhttp post 上传或下载时显示进度条

    通过 idhttp 带进度条上传演示一下,下载和上传原理差不多,说明一下下面例子中的的idhttp 是动态创建的 第一步:添加一个StatusBar或者gauge 进度条,这2个都可以.我用的是 st ...

  10. Delphi的IDHTTP的基本用法

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

随机推荐

  1. 部署.net Core 到 Windows server 2008 r2 IIs

    1. 将项目发布 2.iis 新建网站,设置 3 安装windows  server  hosting 4 关于报错 1.下载sdk https://www.microsoft.com/net/lea ...

  2. Database基础(七):部署集群基础环境、MySQL-MMM架构部署、MySQL-MMM架构使用

    一.部署集群基础环境 目标: 本案例要求为MySQL集群准备基础环境,完成以下任务操作: 数据库授权 部署MySQL双主多从结构 配置本机hosts解析记录 方案: 使用4台RHEL 6虚拟机,如下图 ...

  3. [CSP-S模拟测试]:蛋糕(区间DP)

    题目传送门(内部题34) 输入格式 第一行,一个正整数$n$.第二行,$n$个正整数$a_i$,保证$a_i$互不相等. 输出格式 一行一个整数表示间宫卓司得到的蛋糕大小总和的最大值. 样例 样例输入 ...

  4. springmvc缓存 - cache

    前几篇文章已经搭建了一个基本的springmvc demo,现在我们来完善下.    相信大家写程序的时候都接触过缓存的概念,也都知道,数据量大的时候缓存对于提高效率是很显著的.而缓存一般包括前台静态 ...

  5. sql格式化工具推荐

    还在为上百行甚至上千行冗余的sql烦恼吗?这里推荐一款在线美化sql的工具 工具地址:http://www.matools.com/sql 怎么样,黑屏界面是不是带有满满的黑科技感? 该工具支持ora ...

  6. appium 链接真机运行时报错

    今天用appium链接真机时,碰到的第一个问题:Attempt to re-install io.appium.settings without first uninstalling.(这是日志中显示 ...

  7. C++笔试题之宏定义相关

    1. #define CALC(X) X*X int i; i=CALC(+)/(+); cout<<i<<endl; 输出:31 宏定义在替换处展开为:i = 5+5*5+5 ...

  8. springboot controller传参,对象映射

    Post请求,对象映射时,在参数 加 @RequestBody: 传入对象内字段的json才能映射 {"legendData": [100,90,80,70,60,50,40,30 ...

  9. 查询事件状态,mysql查看事件是否开启,设置启动时自动开启方法

    1.查看事件是否开启 SHOW VARIABLES LIKE 'event_scheduler' 2.设置当前事件开启 SET GLOBAL event_scheduler = 1; 或 SET GL ...

  10. 对 HTTP HTTPS的认识

    1.HTTP:超文本传输协议 -以明文的形式传输 -效率更高,但是不安全 2.HTTPS:HTTP+SSL -传输之前数据先加密,之后在揭秘 -效率低,但是安全 3.get请求和post请求的区别 - ...