delphi - IdHTTP.get 返回 HTTP1.1/403 禁止

标签 delphi indy10 idhttp

我正在尝试使用 DelphiXE 编译的程序和 IdHTTP 组件访问我网站上的 update.txt 文件。

我使用的代码如下:

procedure TFormAbout.SpeedButtonUpdateClick(Sender: TObject);

function CheckUpdates: String;
var lIdHttp: TIdHTTP;
begin
  lIdHttp := TIdHTTP.Create(nil);
  result := lIdHttp.Get('http://www.test.com/test_down_load/update.txt');
end;

var
sWebVersion: String;
sVersionList: TStringList;

begin
try
  sWebVersion := Checkupdates;
except
  on E: Exception do
  begin 
    ShowMEssage(E.ErrorMessage);
    MessageDlg('An Error occured checking for an update.',mtError,[mbOK],0);
  end;
end;
if sWebVersion <> '' then
  begin
    sVersionList.CommaText := sWebVersion;
    ShowMessage('Version: ' + sVersionList[0] + ' - ' + 'Date: ' + sVersionList[1]);
  end;
end;

但这会导致错误:HTTP1.1/403 Forbidden

已使用以下属性设置 IdHTTP 组件。

HandleRedirects := true;
HTTPOptions [hoForceEncodeParams];
ProtocolVersion := pv1_1;
Request.UserAgent := Mozilla/5.0 (compatible; Test)

如果我在 IE 浏览器中输入 URL,它会返回文件而不会出现错误,但是当从我的程序访问时,我会收到错误。 任何指示将不胜感激。 .htaccess 对于该网站来说是正确的。 网站上文件的权限是正确的:0644。

我是否必须为 IdHTTP 组件设置任何其他属性。我只在 about 表单上有这个组件。我还需要什么吗?

updateinfo.txt 文件仅包含引号中的文本: "18.3.5,2011/12/17"

我在这里简单地使用了“test”来代替我的实际程序名称和 URL。

问候 阿德里安

最佳答案

在使用 Indy 的 Get() 函数时,我遇到了完全相同的问题。

您收到此错误的可能性很可能是因为您尚未设置 UserAgent 属性,并且网站知道您没有访问该文件,因为浏览器正在大惊小怪。

function CheckUpdates: String;
var lIdHttp: TIdHTTP;
begin
  lIdHttp := TIdHTTP.Create(nil);
  //avoid getting '403 Forbidden' by setting UserAgent
  lIdHttp.Request.UserAgent :=
      'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
  result := lIdHttp.Get('http://www.test.com/test_down_load/update.txt');
end;

类似的问题,但正确的答案记录在此处: https://stackoverflow.com/questions/10870730/why-do-i-get-403-forbidden-when-i-connect-to-whatismyip-com

关于delphi - IdHTTP.get 返回 HTTP1.1/403 禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546370/

相关文章:

delphi - 同一应用程序上的多个 TIdCmdTCPServers

delphi - Indy 10 + SSL = 适用于 Windows 7,不适用于 XP

delphi - 在 C++ 构建器中使用 TIdAttachment 发送附件

delphi - indy GET 下载速度

delphi - 有没有办法找到 Delphi 编译器版本?

C# 等效 HGlobal Pascal

c# - 如何在C#中声明嵌入式结构(如delphi中的嵌入式记录)?

delphi - idHttp Get + Delphi + 请求的URL被拒绝

Delphi HTTP POST 方法 - Cookie 问题

delphi - 在Delphi 2010中读取ShellExecute()的输出文件?