请教怎么使用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>

解决方案 »

  1.   

    参考
    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;
      

  2.   

    登陆时在IE地址栏里面应该是http://192.168.0.3/longin.aspx?username=123&password=456,请问其中的问好“?”,我应该放在哪里???
    放在PostURL最后:
        PostURL:="http://172.31.5.103/longin.aspx"
    还是参数最前面:
        sParams1 := '?username='+ user;
      

  3.   

    顶一下!
    问题还没有解决,先谢谢楼上的两位!不知为何
      if pos('main.aspx',ResponseStream.DataString)>0 then
          Result:=True
    总不能返回真!!!
      

  4.   

    还是问号的问题,用下面代码总是不成功:
      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,???
      

  5.   

    http://www.delphibbs.com/delphibbs/dispq.asp?lid=2071973
    这个应该会对楼主有所帮助
      

  6.   

    问题基本解决了,谢谢大家请大家解决另外一个问题
    http://community.csdn.net/Expert/topic/4683/4683359.xml?temp=.8455164