How to set proxy host on HttpClient request in Java

前端 未结 4 1137
野趣味
野趣味 2021-01-12 13:13

I want to set proxy before sending HttpClient request on a URL. As I am able to connect it curl command setting up the proxy but with Java code I am not able to

相关标签:
4条回答
  • 2021-01-12 13:46

    Add maven dependency (4.2.X+):

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    

    Use HttpClientBuilder and set flag useSystemProperties:

    HttpClient client = HttpClientBuilder.create().useSystemProperties().build();
    

    then you have two options at least:

    Option A: Override arguments, i.e.

    java -Djava.net.useSystemProxies=true 
         -Dhttp.proxyHost=PROXY_HOST
         -Dhttp.proxyPort=PROXY_PORT 
         -Dhttp.proxyUser=USERNAME
         -Dhttp.proxyPassword=PASSWORD
         -jar your-app.jar
    

    Option B: Set up JVM (${JAVA_HOME}/lib/net.properties):

    java.net.useSystemProxies=true
    http.proxyHost=some-host
    http.proxyPort=some-port
    

    and run your application

    0 讨论(0)
  • 2021-01-12 13:49

    A full example is given in this tutorial: How do I setting a proxy for HttpClient?

    package org.kodejava.example.commons.httpclient;
    
    import org.apache.commons.httpclient.Credentials;
    import org.apache.commons.httpclient.HostConfiguration;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpMethod;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.auth.AuthScope;
    import org.apache.commons.httpclient.methods.GetMethod;
    
    import java.io.IOException;
    
    public class HttpGetProxy {
        private static final String PROXY_HOST = "proxy.host.com";
        private static final int PROXY_PORT = 8080;
    
        public static void main(String[] args) {
            HttpClient client = new HttpClient();
            HttpMethod method = new GetMethod("https://kodejava.org");
    
            HostConfiguration config = client.getHostConfiguration();
            config.setProxy(PROXY_HOST, PROXY_PORT);
    
            String username = "guest";
            String password = "s3cr3t";
            Credentials credentials = new UsernamePasswordCredentials(username, password);
            AuthScope authScope = new AuthScope(PROXY_HOST, PROXY_PORT);
    
            client.getState().setProxyCredentials(authScope, credentials);
    
            try {
                client.executeMethod(method);
    
                if (method.getStatusCode() == HttpStatus.SC_OK) {
                    String response = method.getResponseBodyAsString();
                    System.out.println("Response = " + response);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                method.releaseConnection();
            }
        }
    }
    

    For this, you need to add a jar file: http://repo1.maven.org/maven2/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1.jar

    Complete Example of a Apache HttpClient 4.1, setting proxy can be found below

    HttpHost proxy = new HttpHost("ip address",port number);
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
    
    HttpPost httpost = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("param name", param));
    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
    HttpResponse response = httpclient.execute(httpost);
    
    HttpEntity entity = response.getEntity();
    System.out.println("Request Handled?: " + response.getStatusLine());
    InputStream in = entity.getContent();
    httpclient.getConnectionManager().shutdown();
    

    Resource Link: https://stackoverflow.com/a/4957144

    For other version like 4.3 or 4.3+, you can go through this SO answer: Apache HttpClient 4.1 - Proxy Authentication

    0 讨论(0)
  • 2021-01-12 13:59

    System properties usually have to be set right upfront, which means either in the jvm startup with -Dhttp.proxyHost=some.host.com -Dhttp.proxyPort=8080, not forgetting the httpsProxyHost and ...Port)

    Or which should work, too, in a static {} block.

    I personally find the Jersey client implementation easier to work with, if you have the option to switch.

    0 讨论(0)
  • 2021-01-12 14:01

    I think this could be helpful:

    HttpClient client = new HttpClient();
    
    HostConfiguration config = client.getHostConfiguration();
    config.setProxy("someProxyURL", "someProxyPort");
    
    Credentials credentials = new UsernamePasswordCredentials("username", "password");
    AuthScope authScope = new AuthScope("someProxyURL", "someProxyPort");
    client.getState().setProxyCredentials(authScope, credentials);
    
    EntityEnclosingMethod method = new PostMethod(url);
    method.setRequestEntity(new StringRequestEntity(requestXML, "text/xml", "utf-8"));
    
    0 讨论(0)
提交回复
热议问题