My application needs to connect to my website. The code snippet works perfectly fine on most machines. However some users complain it's not working despite they have internet connection.
uses System.Net.HttpClient;
procedure TForm1.Button1Click(Sender: TObject);
var
HttpClient: THttpClient;
HttpResponse: IHttpResponse;
begin
HttpClient := THTTPClient.Create;
try
try
HttpResponse := HttpClient.Get('http://mywebsite.com');
except
on E:exception do
showmessage(E.Message);
end;
Memo1.Lines.Text := HttpResponse.ContentAsString();
finally
HttpClient.Free;
end;
end;
I can reproduce the error message by using the Sandboxie software. Sandboxie allows internet connection to my application but my application displays error. The error message is exactly this:
Error sending data: (1752) The server endpoint cannot perform the operation
The problem arises when I try both http or https websites.
EDIT: When I try using TIdHttp (see below), the same application can connect. But I want a non-Indy solution.
procedure TForm1.Button2Click(Sender: TObject);
var
PostData: TStringList;
begin
PostData:=TStringlist.Create;
Memo1.Lines.Text:=IdHTTP1.Post('http://mywebsite.com',PostData); // WORKS!
end;
EDIT2: I think I will use TIdHttp (http only) as 2nd try. It will work for everybody.
ContentAsString()
inside thetry
block. IfGet()
raises an exception, the assignment ofHttpResponse
will be skipped, so callingContentAsString()
outside of thetry
block will crash. – Remy Lebeau Nov 28 '20 at 22:21