使用TIdHTTP控件进行页面访问

本文档介绍了如何使用TIdHTTP控件进行网页POST请求,通过设置请求头、附加参数并获取响应内容,从而实现与服务器的交互。在示例中,解析返回的HTML代码以提取关键信息,并根据结果处理错误情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

seabird

使用TIdHTTP控件进行页面访问
procedure MSG(var theuser:TUserInfo);
var
Url: string;
tempstr:widestring;
Response : TStringStream;
Request : TStrings;//用于存贮传向目的页面的信息,随Request传送,将自动封装于Form中
sqlid,code,status:string;
temp:integer;
mystringlist:TStringList;
begin
IdHTTP_main是个TIdHTTP控件
Response := TStringStream.Create('');
Request := TStringList.Create ;
try
Url:='http://coderClub/sms/hz/xx.asp';
Request.Append('seqid='+theuser.SeqID);
Request.Append('mobile='+theuser.Mob);
Request.Append('spcode='+theuser.Spc);
Request.Append('msg='+theuser.Msg);
Service1.IdHTTP_main.Request.ContentType :='application/x-www-form-urlencoded';
Service1.IdHTTP_main.Post(Url,Request,Response);
//Response.DataString存贮着返回页面的html代码,本例以一下方式获得其中信息
tempstr:=LowerCase(Response.DataString);
tempstr:=LeftStr(tempstr,pos('</returndeliver>',tempstr)+16);
temp:=pos('<seqid>',tempstr);
sqlid:=MidStr(tempstr,pos('<seqid>',tempstr)+7,pos('</seqid>',tempstr)-temp-7);
if not IsNumber(sqlid) then
begin
mystringlist:=TStringList.Create;
if FileExists(ExtractFilePath(Paramstr(0))+'Err.txt') then
mystringlist.LoadFromFile(ExtractFilePath(Paramstr(0))+'Err.txt');
mystringlist.Add('('+DateTimeToStr(Now)+')seqid('+sqlid+')未知');
mystringlist.SaveToFile(ExtractFilePath(Paramstr(0))+'Err.txt');
mystringlist.Free;
end
else
begin
code:=MidStr(tempstr,pos('<code>',tempstr)+6,pos('</code>',tempstr)-pos('<code>',tempstr)-6);
if not IsNumber(code) then
begin
mystringlist:=TStringList.Create;
if FileExists(ExtractFilePath(Paramstr(0))+'Err.txt') then
mystringlist.LoadFromFile(ExtractFilePath(Paramstr(0))+'Err.txt');
mystringlist.Add(DateTimeToStr(Now)+'[sqlid:'+sqlid+']错误码未知');
mystringlist.SaveToFile(ExtractFilePath(Paramstr(0))+'Err.txt');
mystringlist.Free;
code:='1';
status:='(错误码未知)';
end;
status:=status+MidStr(tempstr,pos('<status>',tempstr)+8,pos('</status>',tempstr)-pos('<status>',tempstr)-8);
with Service1.ADOQuery_temp do
begin
Connection:=conn_Client;
Close;
SQL.Clear;
SQL.Add('update '+GameInfo.deliver_table_log+' set code='+code+',status='''+status+''' where seqid='+theuser.SeqID);
ExecSQL;
end;

end;
finally
Request.Free ;
Response.Free ;
end;


end;
-----------------------------------------
飘飘何所似?天地一沙鸥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值