简体   繁体   English

URL不返回数据Delphi Indy TIdHttp

[英]URL Not Returning Data Delphi Indy TIdHttp

I am trying to access a URL in Delphi using a TIdHTTP Indy Tool. 我试图使用TIdHTTP Indy工具访问Delphi中的URL。 I have done the following: 我做了以下事情:

  • Set Accept Cookies = True 设置Accept Cookies = True
  • Set Handle Redirect = True 设置句柄重定向=真
  • Added a TIdCookieManager 添加了TIdCookieManager

http://sms.saicomvoice.co.za:8900/saicom/index.php?action=login&username=SOME_USERNAME&password=SOME_PASSWORD&login=login

The Post request works and it returns the HTML. Post请求有效,它返回HTML。 The problem is it doesn't return the correct HTML (See Image Below). 问题是它没有返回正确的HTML(见下图)。

If I take that URL ( Filling in the username and password ) and paste it into my browser exactly The Same as my Delphi Application would then logs into the correct website. 如果我拿到那个URL(填写用户名和密码)并将其完全粘贴到我的浏览器中,那么我的Delphi应用程序就会登录到正确的网站。 But as soon as I do it with my Delphi App it returns the HTML for the login page. 但是,只要我使用我的Delphi应用程序,它就会返回登录页面的HTML。

The request is supposed to be executed timeously in a TTimer in Delphi. 该请求应该在Delphi的TTimer中及时执行。

Can anyone lead me unto the right path or point me in a direction as to how I can solve this problem ? 任何人都可以引导我走向正确的道路,或指出我如何解决这个问题的方向?

Some Additional Information 一些附加信息

  • WriteStatus is a Procedure That writes output to a TListBox WriteStatus是一个将输出写入TListBox的过程
  • BtnEndPoll Stops the timer BtnEndPoll停止计时器

     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输出

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

That is not a valid ContentType value. 这不是有效的ContentType值。 That kind of value belongs in the Request.Accept property instead. 这种值属于Request.Accept属性。 It tells the server which ContentTypes the client will accept in the response. 它告诉服务器客户端将在响应中接受哪些ContentType。

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

You are posting a blank TStringList . 您正在发布空白的TStringList Putting a URL into a browser's address bar sends a GET request, not a POST request, so you should be using TIdHTTP.Get() instead: 将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');

You would use TIdHTTP.Post() if you wanted to simulate the HTML webform being submitted to the server (since it specifies method=post ), eg: 如果要模拟提交给服务器的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM