Apache HttpClient 4.1 - Proxy Authentication

前端 未结 7 1111
走了就别回头了
走了就别回头了 2020-11-30 03:07

I\'ve been trying to configure the user and password for proxy authentication from the configured properties while using Apaches HttpComponent\'s httpclient, but with no suc

7条回答
  •  死守一世寂寞
    2020-11-30 03:54

    If you have to keep your code working with 4.1 or want to use the below snippet, it's important to know that httpclient 4.1 will not send the authentication to proxy. You will probably get a 407 "Proxy Authentication Required" status code. I upgraded to 4.3.3 and all worked well, although DefaultHttpClient and ConnRoutePNames were deprecated in this release. Hope this helps!

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(
        new AuthScope("PROXY HOST", 8080),
        new UsernamePasswordCredentials("username", "password"));
    
    HttpHost proxy = new HttpHost("PROXY HOST", 8080);
    
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    

提交回复
热议问题