0

I have a function (with Host = 'https://api.ipify.org/'):

TRespEx=record
 Resp:IHttpResponse;
 EError:String
 end;

function CheckIp(UseProxy: Boolean = True; Host: String = ipcheck;
  ProxyHost: String = '127.0.0.1'; ProxyPort: Integer = 9090): TRespEx;
var
  Cli: THttpClient;
begin
  Cli := THTTPClient.Create;
  Cli.SecureProtocols := [THTTPSecureProtocol.SSL2, THTTPSecureProtocol.SSL3,
                          THTTPSecureProtocol.TLS1, THTTPSecureProtocol.TLS11,
                          THTTPSecureProtocol.TLS12, THTTPSecureProtocol.TLS13];
  try
    try
      if UseProxy = True then Cli.ProxySettings.Create(ProxyHost, ProxyPort);
      Cli.SetUserAgent(PUserAgent);
      Result.Resp := Cli.Get(Host);
    except
      on E: Exception do
        Result.EError := E.Message;
    end;
  finally
    Cli.Free;
  end;
end;

It worked well wthout proxy(False), but a few days ago it broke down. The exception (E.Message) returns an error:

Error sending data: (12002) The operation time out

I do nothing, I did not change any code. And the function still works, but only with proxy params(True). I know, you say that I blocked the Host, but it still works fine in a browser.

PUserAgent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0'. I just copied it from my browser user agent.

I turned off the antivirus and firewall, but the result is the same.

Any ideas? I don't understand what the problem is.

Bald
  • 13
  • 3
  • 1
    And which value does `PUserAgent` contain? Hosts can also block per user agent. – AmigoJack Sep 08 '22 at 14:11
  • PUserAgent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0'. I just copied it from my browser user agent – Bald Sep 09 '22 at 11:49

1 Answers1

0

If you call the function as you say, then you are using a proxy, which in this case is installed on the local machine (127.0.0.1) and TCP port 9090.

Are you sure that the proxy is still up and running?

If don't want to use this proxy, then you have to call the function with 'False' as the first parameter.

A more simplified code, could be the following:

function CheckIp(UseProxy:Boolean=True; Host:String='https://api.ipify.org/'; ProxyHost:String='127.0.0.1'; ProxyPort:Integer=9090): String;
var
 Cli:   THttpClient;
begin
 Cli:=THTTPClient.Create;
 Cli.SecureProtocols:=[THTTPSecureProtocol.SSL2,THTTPSecureProtocol.SSL3,
                       THTTPSecureProtocol.TLS1,THTTPSecureProtocol.TLS11,
                       THTTPSecureProtocol.TLS12,THTTPSecureProtocol.TLS13];
 try
    if UseProxy=True then Cli.ProxySettings.Create(ProxyHost,ProxyPort);
    Result := Cli.Get(Host).ContentAsString;
 finally
    Cli.Free;
 end;

end;


procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(CheckIP(False, 'https://api.ipify.org/'));
end;

Also, you are saying that when you are using the proxy server, the connection is establishing successfully. But when you are not using the proxy, you are getting the error.

Something like this, is possible if the PC you are working on, has connection issues (eg: firewall, routing table, bad dns configuration etc). In this case, the direct connections from the PC doesn't work, but the connections made through proxy works.

George Betsis
  • 450
  • 2
  • 4
  • TRespEx=record. I need E.Exception in result – Bald Sep 09 '22 at 12:00
  • Sorry, i edited my answer: Function worked fine with FALSE parameter, now it is not working. Now it is only working with proxy (TRUE) – Bald Sep 09 '22 at 14:09