通过以下代码,我得到异常类EIdHTTPProtocolException,并显示'HTTP/1.1 403 Forbidden'消息。流程svchostip.exe(11172)当我连接到whatismyip.com时,为什么会收到“403 Forbidden”?
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;
这有点幸运,否则TIdHTTP会泄漏。 –
您正在将'Host'属性设置为显然不是主机名的东西。它不应该是一个完整的URL。您正在连接* automation.whatismyip.com *,因此也将其用于'Host'属性。 –
你甚至都不应该根本分配'Request.Host'。 'TIdHTTP'在内部管理这个值。 –