Recent

Author Topic: Help Needed With FPHTTPClient & Proxies...  (Read 1673 times)

AaronCatolico1

  • New Member
  • *
  • Posts: 31
Help Needed With FPHTTPClient & Proxies...
« on: December 12, 2024, 06:46:06 pm »
Is it possible to use public or private proxies with the FPHTTPClient? If so, I'd like to see an example.

I've tried a variety of public proxies, especially socks5 proxies from sites such as:

etc.


The error messages I keep getting are "timed out" & "failed to connect", no matter whichever proxy & type I use.

Here's what I'm currently using & have tried:


Code: Pascal  [Select][+][-]
  1. uses
  2.     Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids,
  3.                                 ExtCtrls, ComCtrls, fphttpclient, openssl, opensslsockets;
  4.  
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. var
  8.   HTTPClient: TFPHTTPClient;
  9.   Response: TStringStream;
  10.   URL, SearchTerm: string;
  11.   ProxyHost, ProxyUser, ProxyPass: string;
  12.   ProxyPort:integer;
  13. begin
  14.   // Check if Edit1 (search box) is available and not empty
  15.   if Edit1.Text <> '' then
  16.     SearchTerm := Edit1.Text
  17.   else
  18.   begin
  19.     ShowMessage('Please enter a search term.');
  20.     Exit;
  21.   end;
  22.  
  23.   // Replace spaces in the search term with "+" for URL encoding
  24.   SearchTerm := StringReplace(SearchTerm, ' ', '+', [rfReplaceAll]);
  25.  
  26.   // Construct the Google search URL
  27.   URL := 'https://www.google.com/search?q=' + SearchTerm;
  28.  
  29.   // Proxy configuration
  30.   ProxyHost := '127.0.0.1';  // Example proxy server address
  31.   ProxyPort := 8080;       // Example proxy port
  32.  
  33.  
  34.   // Create HTTP client and response stream
  35.   HTTPClient := TFPHTTPClient.Create(nil);
  36.   Response := TStringStream.Create('');
  37.   try
  38.     try
  39.       // Set proxy settings
  40.         HTTPClient.Proxy.Host := ProxyHost;
  41.         HTTPClient.Proxy.Port := ProxyPort;
  42.  
  43.  
  44.       // Optional: Set a user-agent header to mimic a browser
  45.       HTTPClient.AddHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
  46.  
  47.       // Perform the GET request
  48.       HTTPClient.Get(URL, Response);
  49.  
  50.       // Display the fetched HTML in the TMemo
  51.       Memo1.Lines.Text := Response.DataString;
  52.     except
  53.       on E: Exception do
  54.         ShowMessage('An error occurred: ' + E.Message);
  55.     end;
  56.   finally
  57.     // Free resources
  58.     Response.Free;
  59.     HTTPClient.Free;
  60.   end;
  61. end;


Is the FPHTTPClient not built to use proxies? I haven't been able to find any examples online. AI is what helped me generate this code (as shown above).

I tried the code as shown above to no avail. I was expecting the FPHTTPClient to have proxy support, but not able to make any connections as described above.




« Last Edit: December 13, 2024, 04:49:03 pm by AaronCatolico1 »

 

TinyPortal © 2005-2018