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 ?C 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) ?C 1));
if cookie = ” then cookie := tmp else cookie := cookie + ‘; ‘ + tmp;
end;
if cookie <> ” then
begin
for i := 0 to http.Request.RawHeaders.Count ?C 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;
如何取得网页中的所有连接,对代码做修改你也可以实现查找所有图片等等, QStrings.rar(79K) (点击下载)在这里推荐使用QString来实现文本替换、查找等功能,附件里有下载。

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) ?C 1);
i := Pos(Node, Data);
end;
end;
begin
Result := TStringList.Create;
Count := GetFieldCount(Data, Node);
for i := 0 to Count ?C 1 do
begin
j := Pos(Node, Data);
Result.Add(Copy(Data, 1, j ?C 1));
Delete(Data, 1, j + Length(Node) ?C 1);
end;
Result.Add(Data);
end;
begin
Result := TStringList.Create;
try
List := split(Data, ‘href=’);
for i := 1 to List.Count ?C 1 do
begin
tmp := List[i];
tmp := Copy(tmp, 0, Pos(’</a>’, tmp) ?C 1);
tmp := Copy(tmp, 0, Pos(’>’, tmp) ?C 1);
if Pos(’ ‘, tmp) <> 0 then tmp := Copy(tmp, 0, Pos(’ ‘, tmp) ?C 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;

相关阅读 >>

Delphi 的字符串在 firemonkey 模式下

Delphi 遍历注册表

Delphi编写dll(以及静态和动态方式调用)

Delphi 调整应用程序内存大小

Delphi 图像旋转90° 反旋转90°

Delphi 去掉文件只读属性

Delphi汉字与汉字区位码的转换

Delphi 2010 域名转换ip

Delphi webbroker 上传文件

Delphi map file

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...