It is necessary in the application to implement the ability to work with the network through a proxy server.

I use proxy private, with login and password authorization.

By learning Google could write to such a code

public void proxy_test_auth2() throws IOException { String ntUsername = "a*"; String ntPassword = "***"; String localMachineName = System.getenv("COMPUTERNAME"); String domainName = System.getenv("USERDOMAIN"); String proxyHost = "37.143.11.127"; int proxyPort = 65233; NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword, localMachineName, domainName); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds ); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .setProxy(new HttpHost(proxyHost, proxyPort)) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) .build(); try { HttpGet httpget = new HttpGet("http://control456.ru/sample/save_post_get.php"); System.out.println("Executing request " + httpget.getRequestLine()); // Pass local context as a parameter CloseableHttpResponse response = httpclient.execute(httpget); System.out.println(response.getStatusLine().toString()); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); } finally { response.close(); } } finally { httpclient.close(); } } 

But in return, I get this error

 HTTP/1.0 407 Proxy Authentication Required 

Unfortunately, I can’t figure it out myself, because Not strong in English, but almost all the information about the Apache HTTP Client on it. I suspect that the error is elementary. I would be very grateful to everyone who responded.

    1 answer 1

    Understood. I did it like this, everything works as it should. I think many will come in handy.

     public void proxy_test_auth() throws IOException { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("proxy-ip", proxy_port), new UsernamePasswordCredentials("login", "pass")); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build(); try { HttpHost proxy = new HttpHost("proxy-ip", proxy_port); RequestConfig config = RequestConfig.custom() .setProxy(proxy) .build(); HttpGet httpget = new HttpGet("final site url"); httpget.setConfig(config); System.out.println("Executing request " + httpget.getRequestLine() + " via " + proxy); CloseableHttpResponse response = httpclient.execute(proxy, httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); } finally { //response.close(); } } finally { httpclient.close(); } }