Lê Văn Thanh created HTTPCLIENT-1844:
----------------------------------------

             Summary: CloseableHttpClient error : 
org.apache.http.NoHttpResponseException failed to respond
                 Key: HTTPCLIENT-1844
                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1844
             Project: HttpComponents HttpClient
          Issue Type: Bug
          Components: HttpClient (async)
    Affects Versions: 4.5.3
         Environment: JDK : Java 8
IDE : Netbean
EVR : MacOS
            Reporter: Lê Văn Thanh
         Attachments: log_bug

I tried to using CloseableHttpClient with tor proxy but i got a messenge :
org.apache.http.NoHttpResponseException: The target server failed to respond

My code I using CloseableHttpClient:
Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    HttpGet headMethod = new HttpGet("https://wtfismyip.com/";);
                    HttpHost proxy = new HttpHost("127.0.0.1", 9150);
                    RequestConfig requestConfig = RequestConfig.custom()
                            .setSocketTimeout(5 * 1000)
                            .setConnectTimeout(5 * 1000)
                            .build();
                    headMethod.setConfig(requestConfig);

                    RegistryBuilder<ConnectionSocketFactory> 
connRegistryBuilder = RegistryBuilder.create();
                    connRegistryBuilder.register("http", 
PlainConnectionSocketFactory.INSTANCE);

                    SSLContext sslContext
                            = SSLContexts.custom().loadTrustMaterial(null, new 
TrustStrategy() {
                                @Override
                                public boolean isTrusted(final 
X509Certificate[] chain, String authType) {
                                    return true;
                                }
                            }).build();
                    SSLConnectionSocketFactory sslsf
                            = new SniSSLConnectionSocketFactory(sslContext, 
NoopHostnameVerifier.INSTANCE);
                    connRegistryBuilder.register("https", sslsf);

                    Registry<ConnectionSocketFactory> connRegistry = 
connRegistryBuilder.build();
                    PoolingHttpClientConnectionManager connectionManager = new 
SniPoolingHttpClientConnectionManager(connRegistry);
                    connectionManager.setMaxTotal(10);
                    connectionManager.setDefaultMaxPerRoute(10);

                    CloseableHttpClient httpClient = HttpClientBuilder
                            .create()
                            .setDefaultRequestConfig(requestConfig)
                            .setConnectionManager(connectionManager)
                            .setProxy(proxy)
                            .disableContentCompression()
                            .build();

                    HttpResponse httpResponse = httpClient.execute(headMethod);
                    int statusCode = 
httpResponse.getStatusLine().getStatusCode();
                    System.out.println(statusCode);
                } catch (IOException | NoSuchAlgorithmException | 
KeyStoreException | KeyManagementException ex) {
                    Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, 
null, ex);
                }

            }
        };
        thread.start();

But when i using HttpURLConnection then ok , so I don't know why 
CloseableHttpClient not ok.

My code i using HttpURLConnection :
new Thread(() -> {
            try {
                URL url = new URL("https://wtfismyip.com/";);
                Proxy proxy = new Proxy(Proxy.Type.SOCKS, new 
InetSocketAddress("localhost", 9150));
                HttpURLConnection uc = (HttpURLConnection) 
url.openConnection(proxy);
                uc.setConnectTimeout(10000);
                Map<String, List<String>> map = uc.getHeaderFields();

                System.out.println("status = " + uc.getResponseCode());
                

            } catch (MalformedURLException ex) {
                Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, 
null, ex);
            } catch (IOException ex) {
                Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, 
null, ex);
            }
        }).start();




--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to