delphi 一句话,一分钟,取得网页源码

拖三个组件: memo ,idhttp,edit 加一个Button

然后在procedure TForm1.Button1Click(Sender: TObject);里加上一句话

1.可以这么写

memo1.Text:= idHTTP1.Get(edit1.Text);

2.也可以这么写

memo1.Lines.Add(IdHTTP1.Get(edit1.Text));

这件事就这么简单.

不过,别用它来试百度,因为百度屏蔽了 ‘Indy Library’ 这个客户端标识的。

hehe,除非修改http头
User-Agent: Mozilla/3.0 (compatible; Indy Library) 修改前面绿色的部分

注:如果出现的是乱码,查一下html的文件头,看看编码方式,然后比如改成这样

memo1.Text:=utf8toansi( idHTTP1.Get(edit1.Text));

更进一步:

function TForm1.Get(URL: string):string;
var
IDHTTP: TIDHttp;
ss: String;
begin

IDHTTP:= TIDHTTP.Create(nil);
try
try
idhttp.Disconnect;
   
    idhttp.HandleRedirects:= true;   //必须支持重定向否则可能出错
    idhttp.ReadTimeout:= 30000;     //超过这个时间则不再访问
ss:=utf8toansi( IDHTTP.Get(URL));   //UTF8编码转换
// if ss='' then
    ss:=UTF8Decode(idhttp.Get(url));
    if IDHTTP.ResponseCode=200 then
    Result :=ss;
except
//ss:='';
end;
finally
IDHTTP.Free;
end;
end;

http://hi.baidu.com/sxshandian/blog/item/96d217d6339adb2806088b22.html

原文地址:https://www.cnblogs.com/hackpig/p/1668514.html