请教怎么使用Idhttp控件。
我用delphi写了一个网站登陆验证页面,登陆时总是出现HTTP 1.1/302 Found,然后我跟踪的返回值ResponseStream.DataString='',我只想获得一个验证值,不知道为何网页总是转向default.aspx, 而不是转向我希望的main.aspx,如何解决?
附验证代码:
function TFormLogin.LoginOK: Boolean;
var
IdHTTP1 :TIdHTTP;
sParams1,sParams2,sParams3 :String;
LoginInfo :TStrings;
ResponseStream :TStringStream;
begin
PostURL:="http://172.31.5.103/longin.aspx"
Result :=false;
IdHTTP1 := TIdHTTP.Create(nil);
LoginInfo:= TStringList.Create;
ResponseStream := TStringStream.Create('');
sParams1 := '?username='+ user;
sParams2 := 'password='+ pass;
LoginInfo.Clear;
LoginInfo.Add(sParams1);
LoginInfo.Add(sParams2);
try
IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
IdHTTP1.Request.ContentLength:=length(LoginInfo.Text);
IdHTTP1.Post(PostURL, LoginInfo, ResponseStream);
if pos('验证通过 ',ResponseStream.DataString)>0 then
Result:=True
except
on e:Exception do
MessageBox(e.Message,'提示信息',48);
end;
IdHTTP1.Free;
LoginInfo.Free;
ResponseStream.Free;
end;============================
提交和返回数据:
POST /login.aspx?un=111&up=2228 HTTP/1.0
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
Host: 172.31.5.103
Accept: text/html, */*
User-Agent: Mozilla/3.0 (compatible; Indy Library)
HTTP/1.1 302 Found
Connection: keep-alive
Date: Wed, 05 Apr 2006 00:28:35 GMT
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Location: /pzlq2005/default.aspx
Set-Cookie: ASP.NET_SessionId=55ioo345lzbxdrmyyv15ip55; path=/
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 139<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href='/default.aspx'>here</a>.</h2>
</body></html>
我用delphi写了一个网站登陆验证页面,登陆时总是出现HTTP 1.1/302 Found,然后我跟踪的返回值ResponseStream.DataString='',我只想获得一个验证值,不知道为何网页总是转向default.aspx, 而不是转向我希望的main.aspx,如何解决?
附验证代码:
function TFormLogin.LoginOK: Boolean;
var
IdHTTP1 :TIdHTTP;
sParams1,sParams2,sParams3 :String;
LoginInfo :TStrings;
ResponseStream :TStringStream;
begin
PostURL:="http://172.31.5.103/longin.aspx"
Result :=false;
IdHTTP1 := TIdHTTP.Create(nil);
LoginInfo:= TStringList.Create;
ResponseStream := TStringStream.Create('');
sParams1 := '?username='+ user;
sParams2 := 'password='+ pass;
LoginInfo.Clear;
LoginInfo.Add(sParams1);
LoginInfo.Add(sParams2);
try
IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
IdHTTP1.Request.ContentLength:=length(LoginInfo.Text);
IdHTTP1.Post(PostURL, LoginInfo, ResponseStream);
if pos('验证通过 ',ResponseStream.DataString)>0 then
Result:=True
except
on e:Exception do
MessageBox(e.Message,'提示信息',48);
end;
IdHTTP1.Free;
LoginInfo.Free;
ResponseStream.Free;
end;============================
提交和返回数据:
POST /login.aspx?un=111&up=2228 HTTP/1.0
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
Host: 172.31.5.103
Accept: text/html, */*
User-Agent: Mozilla/3.0 (compatible; Indy Library)
HTTP/1.1 302 Found
Connection: keep-alive
Date: Wed, 05 Apr 2006 00:28:35 GMT
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Location: /pzlq2005/default.aspx
Set-Cookie: ASP.NET_SessionId=55ioo345lzbxdrmyyv15ip55; path=/
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 139<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href='/default.aspx'>here</a>.</h2>
</body></html>
解决方案 »
- Listview改变列宽后竖线没有消失
- 我写的stringgrid删除行操作,求指导
- (100分)《Delphi数据库开发关键技术与实例应用》一书的配套光盘
- 类型库变量问题
- 感谢lihuasoft(坐井观天) ,特开此帖送分。:)
- 关于滚动条定位的问题
- delphi会否没末落(读程序员第九期Borland天才的损失有感)
- GreaterWare 组件发布:瞬间可以改变Ado,bde,dbx等应用为远程系统,安全,快速,原来的代码100%支持,只需要替换一个连接组件就可以了。
- 高手请指教->多个Form间怎么互通消息?
- ljdyn:是不是delphi很容易上手,学的人多了,就没有物以稀为贵的感觉。
- 一个很麻烦的 socket 的问题,请高人指点!
- 关于一个虚拟存储数据,最后取平均值的问题?
procedure TForm1.Button1Click(Sender: TObject);
var
ret: TStringStream;
FmyParams: TStringList;
I: Integer;
IdHTTP1 :TIdHTTP;
begin
FmyParams := TStringList.Create;
ret := TStringStream.Create('');
IdHttp1 := TIdHttp.Create(nil);
try
with FmyParams do
begin
Clear;
Add('para1=123);
Add('para2=456');
end;
try
IdHttp1.Post('http://www.163.com',FmyParams,ret);
except
On E: Exception do
begin end;
end;
I := IdHTTP1.ResponseCode;
finally
ShowMessage(ret.DataString);
IdHTTP1.Disconnect;
FmyParams.Free;
ret.Free;
IdHttp1.Free;
end;
end;
放在PostURL最后:
PostURL:="http://172.31.5.103/longin.aspx"
还是参数最前面:
sParams1 := '?username='+ user;
问题还没有解决,先谢谢楼上的两位!不知为何
if pos('main.aspx',ResponseStream.DataString)>0 then
Result:=True
总不能返回真!!!
PostURL:="http://172.31.5.103/longin.aspx"
LoginInfo.Clear;
LoginInfo.Add('username='+ '123';);
LoginInfo.Add('password='+ '456');
IdHTTP1.Post(PostURL, LoginInfo, ResponseStream);
==============================
如果我把PostURL直接改成我需要用http://192.168.0.3/longin.aspx?username=123&password=456,问题就解决了,请问,访问页面时‘?’问号如何处理,用上面代码中的方法,最后请求的字符时什么,如何才能拼成需要的URL,???
这个应该会对楼主有所帮助
http://community.csdn.net/Expert/topic/4683/4683359.xml?temp=.8455164