我在delphi7下使用TIdHTTP控件登陆一个网站,该网站是使用VS.net建立的,需要提供用户名、密码、登录保留时间等信息,但使用post方法无法正常登录,会是什么问题?
大家可以使用www.1disk.cn验证一下代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
mytemp:TStringStream;
ss:TStringStream;
url:OleVariant;begin
mytemp := TStringStream.Create('username=111&password=222&save=0');
ss := TStringStream.Create('');
try
//IdHTTP1.Request.Referer := 'http://www.1disk.cn/index.aspx';// 引用页,实际上不需要
//IdHTTP1.Request.Accept := 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*';
//IdHTTP1.Request.AcceptLanguage := 'zh-cn';
//IdHTTP1.Request.AcceptEncoding := 'gzip, deflate';
IdHTTP1.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2; CustomExchangeBrowser; .NET CLR 1.1.4322)';
IdHTTP1.Request.ContentType := 'text/html; charset=gb2312';// 执行这句不能得到想要的页面,得到的是相关参数不正确,重登陆的页面。
// 在IE地址栏中直接输入'http://www.xxx.cn/login.aspx?username=111&password=222&save=0'可以正常登录
IdHTTP1.Post('http://www.xxx.cn/login.aspx',mytemp,ss);  //
// 使用sniffer看到提交的数据中没有参数,而且使用的是HTTP/1.0协议,
// 修改IdHTTP的源码使之使用http/1.1协议也没用,仅仅改了协议,参数还是没有提交;
// 返回的代码是100,但在此程序调试得到的代码是200,下面这段调试代码不会执行
if IdHTTP1.ResponseCode = 100 then
begin
  ss.WriteString('go on');
end;//使用get方法肯定不对,该页面要求的是post的数据。执行会报错“HTTP/1.1 501 不支持”。
//IdHTTP1.Get('http:/www.xxx.cn/login.aspx?username=111&password=222&save=0',ss);//IdHTTP1.Get('http://www.sohu.com',ss);  // 测试get方法用,这个网站能返回正确数据Memo2.Lines.Text := ss.DataString; // 如果正常,显示返回页面的源码except
  on e: Exception do
    Memo2.Lines.Add(e.Message);          // 显示错误信息
end;
end;

解决方案 »

  1.   

    logininfo:=TStringlist.Create;
    Response:=TStringStream.Create('');
    try
    logininfo.Add('username=111');
    logininfo.Add('password=222');
    logininfo.Add('submit=登录');
    IdHTTP1.Post('http://bbs.96980.net/member.php',logininfo,Response);
    ...
    Memo1.Lines.Text:=Response.DataString;
      

  2.   

    参考一下:http://community.csdn.net/Expert/TopicView3.asp?id=4749696
      

  3.   

    IdHTTP1.Request.ContentType := 'text/html; charset=gb2312'; // 这句不对,删除就可以了或者
    IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
      

  4.   

    直接在IE地址栏中输入“http://www.1disk.cn/login.aspx?username=111&password=222&save=0”,抓包看到的是
    GET /login.aspx?username=janthan&password=1za2se3hng&save=0 HTTP/1.1
    Accept: */*
    Accept-Language: zh-cn
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)
    Host: www.1disk.cn
    Connection: Keep-Alive
    Cookie: ASP.NET_SessionId=ymumkkze5mdjiy450tuvn0ea; CheckCode=8A9B284CB4824E9F而在delphi中执行抓包看到的是
    POST /login.aspx HTTP/1.0
    Connection: keep-alive
    Content-Type: text/html
    Content-Length: 351
    Host: www.1disk.cn
    Accept: text/html, */*
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2; CustomExchangeBrowser; .NET CLR 1.1.4322)username=janthan&password=1za2se3hng&save=0&__VIEWSTATE=/wEPDwUKMTcyOTg0OTI5MQ9kFgJmD2QWAgIHDxYCHgVzdHlsZQUMZGlzcGxheTpub25lZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUMSW1hZ2VCdXR0b24xBQbJTyYtlf3ourkm7fxeOcmpYzw=&__EVENTVALIDATION=/wEWCgLEyScCr6bqtggC0sXgkQ8C0ZmQhQYC2beJ5wUCxreJ5wUCwLeJ5wUCxLfJ5AUC5+SprQcC0sKZ0wj1f8FDhWTND1Zh/b9/60FHsmgDzQ==我输入的参数跑到后一段去了,该页面有两个隐藏属性__VIEWSTATE和__EVENTVALIDATION。难道与cookie有关?
    各位老大,还有什么招数啊?
      

  5.   

    To:gwbasic(笑笑) 
    若使用
    IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
    在我加上页面的两个隐藏属性后会报错 500 Internal Server Error,大概是属性值开头的“/”引起的吧。
      

  6.   

    Cookie: ASP.NET_SessionId=ymumkkze5mdjiy450tuvn0ea; CheckCode=8A9B284CB4824E9F
      

  7.   

    请把你的代码贴全一点,500 Internal Server Error 应该是服务器端错误getit911(Windows转Linux中)logininfo:=TStringlist.Create;
    Response:=TStringStream.Create('');
    try
    logininfo.Add('username=111');
    logininfo.Add('password=222');
    logininfo.Add('submit=登录');
    IdHTTP1.Post('http://bbs.96980.net/member.php',logininfo,Response);
    ...
    Memo1.Lines.Text:=Response.DataString;这代代码是可以的,你参考一下
      

  8.   

    to:gwbasic(笑笑)我的代码只有一个函数,就是我贴的那段。
    给我参考的代码没用,结果是一样的。http://bbs.96980.net 这个站点我这里也没法打开。
      

  9.   

    把你的网页贴上来看看什么内容,只要得到用户名称和口令参数字段(有的要action字段,比如CSDN),添到logininfo中基本上都可以,可能还要校验码。
      

  10.   

    to:getit911(Windows转Linux中) 
    直接用 http://www.1disk.cn/login.aspx 实验就行了。需要从中提取其它的什么登录元素你可以分析一下。我的代码对于 http://www.delphiun.com/login.asp 这个页面是可行的。
      

  11.   

    问题已经解决。
    “登录”按钮是“ImageButten”类型的,它的x、y坐标是两个隐藏参数,需要提交;而且“__VIEWSTATE”参数必须是第一个参数。谢谢各位的帮忙。主要从keyz(keyz) 的参考页得到提示,谢谢。