d7的IdHTTP怎么实现访问带有访问用户名和密码的网页?并能将访问的内容保存下来呢?有的网页是需要输入参数的!如:http://aaa/mb2.php?recid=2004高手们请回答。已开两贴,答对200奉送!
解决方案 »
- DBGRID上的数据怎么动态更新???
- 求助:通用动态调用Form的过程怎样编写????
- BDE驱动在哪在什么地方找?
- dbgrideh中,字段的displayformat属性能直接将小数转成百分比吗?
- 急急急求救令
- 如何动态创建并显示子窗口
- 怎么清除ClientDataSet的日志?
- sqlserver的时间问题
- 急呀!这是什么错误!编译提示:[warning]backup.pas[7]:unit 'filectrl' is specific to a platform
- 哪有delphi的扫描仪控件,急需
- 這代碼錯在哪? 急救! 急救! 急救! 急救! 急救! 急救! 急救!
- delphi6的NMHTTP或delphi7的IDHTTP保存网页的问题?
代码片断:const
LoginUrl='http://www.csdn.net/member/logon.asp';
PostUrl='http://community.csdn.net/Expert/PostNew_SQL.asp';
ReplyUrl='http://community.csdn.net/Expert/reply.asp';
MsgUrl='http://community.csdn.net/message_board/postsend.asp';MyCookList:全局变量,取得当前用户的Cookie
IdHTTP1: TIdHTTP;登录:
function Logon(UserName, PassWord, CookieTime: string):boolean;
var
LoginInfo: TStrings;
Response: TStringStream;
i: Integer;
Cookie:string;
begin
Result :=False;
Cookie:='';
MyCookList :='';
Response := TStringStream.Create('');
LoginInfo := TStringList.Create;
try
LoginInfo.Clear;
LoginInfo.Add('login_name='+UserName);
LoginInfo.Add('password='+PassWord);
LoginInfo.Add('from=http://community.csdn.net/Expert/Forum.asp');
LoginInfo.Add('cookietime='+CookieTime);
LoginInfo.Add('x=0');
LoginInfo.Add('y=0');
IdHTTP1.Request.Referer:='http://www.csdn.net/member/logon.asp';
IdHTTP1.Request.From :='http://community.csdn.net/Expert/Forum.asp';
try
IdHTTP1.Post(LoginUrl,LoginInfo,Response);
except
showmessage('登陆失败');
end;
showmessage(Response.DataString);
//从返回的页面中找出cookie
for i :=0 to IdHTTP1.Response.RawHeaders.Count-1 do
begin
if UpperCase(Copy(IdHTTP1.Response.RawHeaders[i],1,10)) = 'SET-COOKIE' then
begin
Cookie :=Trim(Copy(IdHTTP1.Response.RawHeaders[i],12,MAXINT));
Cookie :=Copy(Cookie,1,Pos(';',Cookie));
MyCookList :=MyCookList+Cookie;
// showmessage(Cookie);
end;
end;
IdHTTP1.Request.RawHeaders.Add('Cookie: '+MyCookList);
finally
LoginInfo.Free;
Response.Free;
end;
if length(MyCookList)>200 then
result:=True;
end;//回复
function Reply(TopicID, Content: string): boolean;
var
ReplyInfo: TStrings;
Response: TStringStream;
begin
Result :=False;
ReplyInfo := TStringList.Create;
Response :=TStringStream.Create('');
try
begin
//取回复页面
ReplyInfo.Clear;
ReplyInfo.Add('Topicid='+TopicID);
ReplyInfo.Add('xmlReply=aaaaa');
ReplyInfo.Add('csdnname=');
ReplyInfo.Add('csdnpassword=');
ReplyInfo.Add('ReplyContent='+Content); IdHTTP1.Request.CustomHeaders.Add('Cookie: '+copy(MyCookList,1,length(MyCookList)-1));
IdHTTP1.Request.Referer :='http://community.csdn.net/Expert/xsl/Reply_Xml.asp Topicid='+TopicID;
IdHTTP1.Request.UserAgent:='Redhat/9.0';
try
IdHTTP1.Post(ReplyUrl,ReplyInfo,Response);
except
showmessage('回复失败');
exit;
end;
// showmessage(Response.DataString);
if pos('添加完成,正在生成静态页面,请稍候',Response.DataString)>0 then
Result :=true;
end;
finally
ReplyInfo.Free;
Response.Free;
end;
end;//发贴
function PostNew(RoomID, Point, TopicName,
Content: string): boolean;
var
PostInfo: TStrings;
Response: TStringStream;
begin
Result :=False;
PostInfo := TStringList.Create;
Response :=TStringStream.Create('');
try
begin
//取发贴页面
//typestate=1&Point=20&TopicName=test&Room=1404&Content=111222
PostInfo.Clear;
PostInfo.Add('typestate=1');
PostInfo.Add('Point='+Point);
PostInfo.Add('TopicName='+TopicName);
PostInfo.Add('Room='+RoomID);
PostInfo.Add('Content='+Content);
IdHTTP1.Request.CustomHeaders.Add('Cookie: '+copy(MyCookList,1,length(MyCookList)-1));
IdHTTP1.Request.CacheControl:='no-cache';
IdHTTP1.Request.UserAgent:='Windows Advanced Server/5.0';
try
IdHTTP1.Post(PostUrl,PostInfo,Response);
except
showmessage('发帖失败');
exit;
end;
// showmessage(Response.DataString);
if pos('增加成功,请稍候,正在生成静态页面',Response.DataString)>0 then
Result :=true;
end;
finally
PostInfo.Free;
Response.Free;
end;
end;//发短信
function SendMsg(SendTo, Content: string): boolean;
var
PostInfo: TStrings;
Response: TStringStream;
begin
Result :=False;
PostInfo := TStringList.Create;
Response :=TStringStream.Create('');
try
begin
PostInfo.Clear;
PostInfo.Add('Sendto='+SendTo);
PostInfo.Add('Content='+Content);
IdHTTP1.Request.CustomHeaders.Add('Cookie: '+copy(MyCookList,1,length(MyCookList)-1));
try
IdHTTP1.Post(MsgUrl,PostInfo,Response);
except
showmessage('发送失败');
exit;
end;
// showmessage(Response.DataString);
if pos('发送成功',Response.DataString)>0 then
Result :=true;
end;
finally
PostInfo.Free;
Response.Free;
end;
end;
为什么在D6中的Indy示例中的HTTPClient 在D7中没有了。并且D6中的例子,在D7中也不能用了?
我对你的这个用法还不是很清楚,因为我不知道要访问的网站是用什么来验证的
login_name和password是验证页的变量吧?
关键是我要保存查看的网页的内容:我用如下代码。但是登录不进去。返回是没有授权查看网页,麻烦再给我说说!
IdHTTP.Request.Username := edUsername.Text;
IdHTTP.Request.Password := edPassword.Text;
memoHTML.Lines.Text := IdHTTP.Get(cbURL.Text);
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20999625.html還有,
>>为什么在D6中的Indy示例中的HTTPClient 在D7中没有了。并且D6中的例子,在D7中也不
indy不同版本, 有些改動!!