Search code examples
httpproxyjoddconnect-timeout

The connectionTimeout seems not work after setting the proxy in jodd-http(6.0.2)


Here is my code

import jodd.http.*;
import jodd.http.net.*;
public class JoddQuestion {
    public static void main(String[] args) {
        SocketHttpConnectionProvider connectionProvider = new SocketHttpConnectionProvider();
        ProxyInfo proxyInfo = ProxyInfo.httpProxy("xxxx", xx, "xxxx", "xxxx");
        connectionProvider.useProxy(proxyInfo);

        String url = "http://www.google.com";
        HttpResponse response = HttpRequest.get(url).open(connectionProvider).connectionTimeout(1000).timeout(1000).followRedirects(true).send();
        System.out.print(response.bodyText());
    }
}

Google website is blocked by firewall in China. Without setting the proxy, run the program, connectionTimeout works.

HttpResponse response = HttpRequest.get(url).connectionTimeout(1000).timeout(1000).followRedirects(true).send();

enter image description here

However, after setting the proxy, connectionTimeout does not work and program just keeps trying.

HttpResponse response = HttpRequest.get(url).open(connectionProvider).connectionTimeout(1000).timeout(1000).followRedirects(true).send();

enter image description here


Solution

  • The open() method opens the connection (and therefore applies the previously set timeouts. Anything set after the call to open() will not be applied.

    You probably want to use the method: withConnectionProvider() instead of open() - it will just set the provider and not open the connection. Then the timeout will be applied when the connection is actually opened.

    Read more here: https://http.jodd.org/connection#sockethttpconnectionprovider

    Or just use open() as the last method before sending. But I would strongly avoid using open without a good reason: just use send() as it will open the connection.

    EDIT: please upgrade to Jodd HTTP v6.0.6 to prevent some non-related issues, mentioned in the comments.