gpt4 book ai didi

delphi - 为什么当我连接到 Whatismyip.com 时会收到 "403 Forbidden"?

转载 作者:行者123 更新时间:2023-12-03 15:04:09 25 4
gpt4 key购买 nike

使用以下代码,我得到异常类 EIdHTTPProtocolException 和消息“HTTP/1.1 403 Forbidden”。进程 svchostip.exe (11172)

function GetInternetIP:string;
var
IdHTTPMainUrl : TIdHTTP;
begin
try
IdHTTPMainUrl := TIdHTTP.Create(nil);
IdHTTPMainUrl.Request.Host := 'http://www.whatismyip.com/automation/n09230945.asp';
Result := idHTTPMainUrl.Get('http://automation.whatismyip.com/n09230945.asp');
except
IdHTTPMainUrl.Free;
end;
end;

最佳答案

您需要设置您的用户代理,这记录在 WhatIsMyIP faq 中:

•Please set your program's user agent to Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 , this will keep your program from being blocked by CloudFlare

此外,释放 TIdHTTP 实例应该是无条件的,只有在引发异常时才释放它。使用异常处理来处理异常。

function GetInternetIP:string;
var
IdHTTPMainUrl : TIdHTTP;
begin
IdHTTPMainUrl := TIdHTTP.Create(nil);
try
IdHTTPMainUrl.Request.UserAgent :=
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
Result := idHTTPMainUrl.Get('http://automation.whatismyip.com/n09230945.asp');
finally
IdHTTPMainUrl.Free;
end;
end;

关于delphi - 为什么当我连接到 Whatismyip.com 时会收到 "403 Forbidden"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10870730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com