URL未返回数据 Delphi Indy TIdHttp

3
我正在尝试使用TIdHTTP Indy工具在Delphi中访问URL。我已经完成了以下操作:
- 设置Accept Cookies = True - 设置Handle Redirect = True - 添加TIdCookieManager Post请求有效并返回HTML。问题是它没有返回正确的HTML(请参见下面的图像)。
如果将该URL(填写用户名和密码)粘贴到浏览器中,与我的Delphi应用程序完全相同,则可以正确登录网站。但是,一旦我在Delphi应用程序中执行此操作,它就会返回登录页面的HTML。
请求应该及时在Delphi的TTimer中执行。
有人能否指导我走上正确的道路或指导我如何解决这个问题?
一些附加信息:
  • WriteStatus is a Procedure That writes output to a TListBox
  • BtnEndPoll Stops the timer

    Procedure TfrmMain.TmrPollTimer(Sender: TObject);
    Var
      ResultHTML: String;
      DataToSend: TStringList;
    Begin
      Inc(Cycle, 1);
    
      LstStatus.Items.Add('');
      LstStatus.Items.Add('==================');
      WriteStatus('Cycle : ' + IntToStr(Cycle));
      LstStatus.Items.Add('==================');
      LstStatus.Items.Add('');
    
      DataToSend := TStringList.Create;
    
      Try
        WriteStatus('Setting Request Content Type');
        HttpRequest.Request.ContentType := 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
        WriteStatus('Setting Request User Agent');
        HttpRequest.Request.UserAgent := 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.0b8';
    
        WriteStatus('Posting Request');
        ResultHTML := HttpRequest.Post(FPostToURL, DataToSend);
        WriteStatus('Writing Result');
        FLastResponse := ResultHTML;
    
        WriteStatus('Cycle : ' + IntToStr(Cycle) + ' -- FINISHED');
        LstStatus.Items.Add('');
      Except
        On E: Exception Do
          Begin
            MakeNextEntryError := True;
            WriteStatus('An Error Occured: ' + E.Message);
    
            If ChkExceptionStop.Checked Then
              Begin
                BtnEndPoll.Click;
                WriteStatus('Stopping Poll Un Expectedly!');
              End;
          End;
      End;
    End;
    

* Image Example *

HTML Output


1
你能否将HTML中相关部分包含为文本呈现? - mjn
1个回答

4

HttpRequest.Request.ContentType := 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8';

这不是一个有效的ContentType值。这种类型的值应该放在Request.Accept属性中,它告诉服务器客户端可以接受哪些ContentType。

ResultHTML := HttpRequest.Post(FPostToURL, DataToSend);

你正在提交一个空的TStringList。将URL放入浏览器地址栏会发送GET请求,而不是POST请求,因此您应该使用TIdHTTP.Get()代替:

ResultHTML := HttpRequest.Get('http://sms.saicomvoice.co.za:8900/saicom/index.php?action=login&username=SOME_USERNAME&password=SOME_PASSWORD&login=login');

如果您需要模拟 HTML webform 提交到服务器(因为它指定了 method=post),那么您可以使用 TIdHTTP.Post() 方法,例如:

DataToSend.Add('username=SOME_USERNAME');
DataToSend.Add('password=SOME_PASSWORD');
DataToSend.Add('login=Login');
ResultHTML := HttpRequest.Post('http://sms.saicomvoice.co.za:8900/saicom/index.php?action=login', DataToSend);

我已经添加了Get请求,但由于某种原因,它似乎没有发布实际数据,因为它仍然返回登录页面的HTML。 - user3564246
没事了,我找到问题并解决了。谢谢你的解决方案有所帮助。 - user3564246

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接