My proxy is configured correctly because, apart from the swarm agent, all other programs have successful connections
No. All other programs having successful connections does not guarantee that your proxy server is configured correctly. For example, Jenkins Remoting (and therefore Swarm) requires the ability to issue a CONNECT to a non-standard port, but some proxy servers (including Squid) deny CONNECT to ports other than port 443 by default. The point is, as I explained in my previous comment, that you need to check your proxy server logs for access denied messages, then configure your proxy server appropriately.
No. It sometimes doesn't work. It does work in my local testing. I created a Squid proxy with docker run --name squid -d --publish 3128:3128 sameersbn/squid:3.5.27-2. Then I configured it as follows:
I then was able to successfully connect to Jenkins through the proxy by running java -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=3128 -jar swarm-client.jar -master <my-url> -username <my-username> -password <my-password>.
You probably have not configured your proxy server correctly. I suggest reading the logs of your proxy server to ensure it is not denying any requests from the Swarm client.