通过以下代码,我得到异常类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中进行了介绍:
同样,释放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/