Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1966

System Proxy - HTTP and SOCKS

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 4.5.5, 4.5.6
    • Fix Version/s: None
    • Component/s: HttpClient (Windows)
    • Labels:
      None

      Description

      Hello,

      I have created a proxy server basing on squid solution. And I have hidden the server (Apache Tomcat 8) which I would like to access behind this proxy (it's not available from my local machine directly). Then, I have created an example project which only connects to this hidden server basing on Apache HttpClient libraries:

      • org.apache.httpcomponents:httpcore:4.4.11
      • org.apache.httpcomponents:httpclient:4.5.6

      The code looks like:

      public static void main(String ...args) throws Exception {
      	System.setProperty("https.proxyHost", "<proxy_url>");
      	System.setProperty("https.proxyPort", "<proxy_port>");
      	System.setProperty("socksProxyHost", "<proxy_url>");
      	System.setProperty("socksProxyPort", "<proxy_port>");
      
      	URI uri = new URI("https://<hidden_server_url>");
      	try (CloseableHttpClient build = HttpClientBuilder.create()
      			.useSystemProperties()
      			.build()) {
      		final HttpUriRequest uriRequest = RequestBuilder.get()
      				.setUri(uri)
      				.build();
      		try (CloseableHttpResponse response = build.execute(uriRequest)) {
      			final StatusLine statusLine = response.getStatusLine();
      			System.out.println(statusLine.toString());
      		}
      	}
      }
      

      Unfortunately, as squid does not support SOCKS protocol, opening socket and waiting for response hangs the program. I was thinking that maybe I'm basing on invalid server configuration however when using below code everything works fine for same system properties:

      public static void main(String ...args) throws Exception {
      	System.setProperty("https.proxyHost", "<proxy_url>");
      	System.setProperty("https.proxyPort", "<proxy_port>");
      	System.setProperty("socksProxyHost", "<proxy_url>");
      	System.setProperty("socksProxyPort", "<proxy_port>");
      
      	URI uri = new URI("https://<hidden_server_url>");
      	try (InputStream stream = uri.toURL().openStream()){
      		String s = IOUtils.toString(stream, "UTF-8");
      		System.out.println(s != null);
      	} catch (IOException e) {
      		e.printStackTrace();
      	}
      }
      

      So java itself handles it correctly. What is more if I simply remove the socksProxyHost and socksProxyPort settings, then Apache HttpClient connects correctly to the hidden server. I don't think that having both HTTPS proxy configuration and SOCKS is incorrect as according to Oracle documentation:

      https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html

      "Now, what happens when both a SOCKS proxy and a HTTP proxy are defined? Well the rule is that settings for higher level protocols, like HTTP or FTP, take precedence over SOCKS settings. So, in that particular case, when establishing a HTTP connection, the SOCKS proxy settings will be ignored and the HTTP proxy will be contacted. Let's look at an example:"
      

      As in Apache HttpClient I'm using system properties (useSystemProperties) I would expect that it is handled the same as Oracle does (the SOCK proxy should be ignored). I also checked it for simple HTTP proxy (http.proxyHost, http.proxyPort) and it acts the same (fails).

      Could you please help me with this issue?

      Best regards,
      Mikolaj Broniszewski

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mikolajb Mikolaj Broniszewski
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: