Category Archives: apache-httpclient-4.x

java.io.IOException connection reset by peer in asynchronous http client

I am using the Apache http asynchronous client library to make parallel HTTP calls and receive the response on a callback. I am getting the following error after my request when the code is deployed on the server.

java.io.IOException: Connection reset by peer
    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:197)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:384)
    at org.apache.http.nio.reactor.ssl.SSLIOSession.receiveEncryptedData(SSLIOSession.java:450)
    at org.apache.http.nio.reactor.ssl.SSLIOSession.isAppInputReady(SSLIOSession.java:504)
    at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:120)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
    at java.lang.Thread.run(Thread.java:745)

here are my dependencies

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4</version>
    </dependency>

how do I solve this? Thanks in advance.

Adding my code for reference

        CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().build();
        try {
            httpclient.start();
            final HttpGet[] requests = new HttpGet[] {new HttpGet("https://www.api.com/api/api1"),
                    new HttpGet("https://www.api.com/api/api2"),
                    new HttpGet("https://www.api.com/api/api3")};
            final CountDownLatch latch = new CountDownLatch(requests.length);
            final LocationData location= new LocationData();
            for (final HttpGet request: requests) {
                request.addHeader("Authorization", "Bearer "+token);
                httpclient.execute(request, new FutureCallback<HttpResponse>() {

                    @Override
                    public void completed(final HttpResponse response) {

                        latch.countDown();

                            BufferedReader in = null;
                            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                            String data = null;

                            while((data =in.readLine())!= null)
                            {
                            //process the data
                            }
                    }


                    @Override
                    public void failed(final Exception ex) {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + "->" + ex);
                        ex.printStackTrace();
                    }

                    @Override
                    public void cancelled() {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + " cancelled");
                    }

                });
            }
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //some more processinng here 
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } 

Anyhelp would be appriciated. Anyone?

EDIT adding the logs from the server

status code | last accessed at             | api

200         | 2018-05-16 10:42:05.241000+0000 |  /api1
499         | 2018-05-16 10:42:05.290000+0000 | /api2
499         | 2018-05-16 10:42:05.416000+0000 |   /api3
200         | 2018-05-16 10:42:05.255000+0000 |  /api1
499         | 2018-05-16 10:42:05.415000+0000 | /api2
499         | 2018-05-16 10:42:05.425000+0000 |   /api3
499         | 2018-05-16 10:42:05.416000+0000 |  /api1
200         | 2018-05-16 10:42:05.256000+0000 | /api2
499         | 2018-05-16 10:42:05.460000+0000 |   /api3

Async response streaming with Apache Async Http client

I am using apache async http client to stream objects from azure storage.

I only need to return the HttpResponse object which has the stream associated. My clients will actually have to read from that stream to store the file locally.

So Apache Async clients use a BasicAsyncResponseConsumer which actually buffers the entire file in local memory before calling the completed callback.

I am trying to create my own implementation of AbstractAsyncResponseConsumer so that I can stream the response body instead of actually storing it first but have been unsuccessful to do so till now.

Here is the bare bones cosumer class for reference ->

public class MyConsumer extends` AbstractAsyncResponseConsumer<HttpResponse> {
@Override
protected void onResponseReceived(HttpResponse response) throws HttpException, IOException {

}

@Override
protected void onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException {

}

@Override
protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException {

}

@Override
protected HttpResponse buildResult(HttpContext context) throws Exception {
    return null;
}

@Override
protected void releaseResources() {

}

}

And here is the code to send the request and return the response ->

public void getFile(HttpRequestBase request) {

    MyConsumer myConsumer = new MyConsumer();
    HttpAsyncRequestProducer producer = 
    HttpAsyncMethods.create(request);
    CompletableFuture<HttpResponse> future = new CompletableFuture<>();
    return Future<HttpResponse> responseFuture = 
    httpclient.execute(producer,myConsumer,                                                                                                                   
    new FutureCallback<HttpResponse>() {
      @Override
      public void completed(HttpResponse result) {
     //This is called only when all the response body has been read
     //future.complete(Result)

      }
      @Override                                                                      
      public void failed(Exception ex) {
      }
      @Override
      public void cancelled() {                                                                       
      }
   });

return future;

 }

I will be returning a CompletableFuture of the HttpResponse object to my clients.

They shouldnt be waiting for my http client to read all the response body first in local buffer.

They ideally should start copying directly from the stream provided in the response object.

What should I add inmy implementation of the consumer to get the desired result ?

Getting server certificates from Apache Http Client 4.5

The following code executes correctly about 90% of the time. The other 10% of the time the routedConnection is no longer open and a ShutdownException is thrown when calling getSSLSession(). Are there other ways to get the server certificates before the connection is closed?

Note: Even though the connection is closed at the time this code executes, the response is processed correctly by the client. There is no error to speak of, the connection is just closed.

public class HttpSslResponseInterceptor implements HttpResponseInterceptor {

    public static final String PEER_CERTIFICATES = "PEER_CERTIFICATES";

    private static final GGLogger LOG = GGLogger.getLogger(GGHttpSslResponseInterceptor.class);

    public GGHttpSslResponseInterceptor() {

    }

    @Override
    public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {

        context.setAttribute(PEER_CERTIFICATES, null);

        final ManagedHttpClientConnection routedConnection = (ManagedHttpClientConnection) context.getAttribute(HttpCoreContext.HTTP_CONNECTION);

        if (routedConnection.isOpen() ) {

            final SSLSession sslSession = routedConnection.getSSLSession();

            if (sslSession != null) {

                final Certificate[] certificates = sslSession.getPeerCertificates();

                context.setAttribute(PEER_CERTIFICATES, certificates);
            }

        }else{
            LOG.warn("process: Connection is not open.");
        }

    }
}

Apache HttpClient doesn’t respect timeout?

I created a simple program to test timeouts

private static int TIMEOUT = 20 * 60 * 1000;
private static HttpHost target = new HttpHost("google.com", 81, "http");


public static void main(String[] args) throws IOException {

    final PoolingHttpClientConnectionManager cm =new PoolingHttpClientConnectionManager();
    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setAuthenticationEnabled(true).setConnectTimeout(TIMEOUT)
            .setSocketTimeout(TIMEOUT).build();;
    CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm)
            .setDefaultRequestConfig(defaultRequestConfig).build();

    final HttpGet get = new HttpGet();

    final HttpResponse response = client.execute(target, get, new BasicHttpContext());

    System.out.println(response);
}

But it fails with timeout after 2 minutes, when I set it to 20 minutes. Why it doesn't respect 20 minutes? P.S. I also tried with local IP address, and iptables that drops packets to simulate timeout.

2017-06-30 21:02:35 DEBUG RequestAddCookies:122 - CookieSpec selected: default
2017-06-30 21:02:35 DEBUG RequestAuthCache:76 - Auth cache not set in the context
2017-06-30 21:02:35 DEBUG PoolingHttpClientConnectionManager:249 - Connection request: [route: {}->http://google.com:81][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
2017-06-30 21:02:35 DEBUG PoolingHttpClientConnectionManager:282 - Connection leased: [id: 0][route: {}->http://google.com:81][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
2017-06-30 21:02:35 DEBUG MainClientExec:234 - Opening connection {}->http://google.com:81
2017-06-30 21:02:35 DEBUG DefaultHttpClientConnectionOperator:138 - Connecting to google.com/173.194.222.113:81
2017-06-30 21:02:56 DEBUG DefaultHttpClientConnectionOperator:167 - Connect to google.com/173.194.222.113:81 timed out. Connection will be retried using another IP address
2017-06-30 21:02:56 DEBUG DefaultHttpClientConnectionOperator:138 - Connecting to google.com/173.194.222.102:81
2017-06-30 21:03:17 DEBUG DefaultHttpClientConnectionOperator:167 - Connect to google.com/173.194.222.102:81 timed out. Connection will be retried using another IP address
2017-06-30 21:03:17 DEBUG DefaultHttpClientConnectionOperator:138 - Connecting to google.com/173.194.222.101:81
2017-06-30 21:03:38 DEBUG DefaultHttpClientConnectionOperator:167 - Connect to google.com/173.194.222.101:81 timed out. Connection will be retried using another IP address
2017-06-30 21:03:38 DEBUG DefaultHttpClientConnectionOperator:138 - Connecting to google.com/173.194.222.100:81
2017-06-30 21:03:59 DEBUG DefaultHttpClientConnectionOperator:167 - Connect to google.com/173.194.222.100:81 timed out. Connection will be retried using another IP address
2017-06-30 21:03:59 DEBUG DefaultHttpClientConnectionOperator:138 - Connecting to google.com/173.194.222.139:81
2017-06-30 21:04:20 DEBUG DefaultHttpClientConnectionOperator:167 - Connect to google.com/173.194.222.139:81 timed out. Connection will be retried using another IP address
2017-06-30 21:04:20 DEBUG DefaultHttpClientConnectionOperator:138 - Connecting to google.com/173.194.222.138:81
2017-06-30 21:04:41 DEBUG DefaultManagedHttpClientConnection:98 - http-outgoing-0: Shutdown connection
2017-06-30 21:04:41 DEBUG MainClientExec:128 - Connection discarded
2017-06-30 21:04:41 DEBUG PoolingHttpClientConnectionManager:320 - Connection released: [id: 0][route: {}->http://google.com:81][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connect to google.com:81 [google.com/173.194.222.113, google.com/173.194.222.102, google.com/173.194.222.101, google.com/173.194.222.100, google.com/173.194.222.139, google.com/173.194.222.138] failed: Connection timed out: connect

MultipartEntityBuilder throwing error on build method

I am posting video to twitter api in segment but MultipartEntityBuilder throwing error on build method while initially it was working fine but suddenly it's throwing error on below line

multipart = builder.build();

Code.

public int uploadMediaAppend(String mediaId, File file)
{
    int code = 0;
    String resp = "";
    String url = "https://upload.twitter.com/1.1/media/upload.json";

    MultipartEntityBuilder builder = null;
    HttpEntity multipart = null;
    CloseableHttpResponse response = null;
    HttpEntity responseEntity = null;

    CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(this.consumerKey, this.consumerSecret);
    consumer.setTokenWithSecret(at.getToken(), at.getTokenSecret());

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost uploadFile = new HttpPost(url);

    try
    {
        if (file.length() < 1 * 1024 * 1024)
        {
            builder = MultipartEntityBuilder.create();
            builder.addTextBody("command", "APPEND", ContentType.TEXT_PLAIN);
            builder.addTextBody("media_id", mediaId, ContentType.TEXT_PLAIN);
            builder.addTextBody("segment_index", "0", ContentType.TEXT_PLAIN);

            builder.addBinaryBody("media", new FileInputStream(file), ContentType.APPLICATION_OCTET_STREAM, file.getName());

            multipart = builder.build();
            uploadFile.setEntity(multipart);

            consumer.sign(uploadFile);

            response = httpClient.execute(uploadFile);
            responseEntity = response.getEntity();

            code = response.getStatusLine().getStatusCode();

        }
        else
        {
            BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));

            builder = MultipartEntityBuilder.create();

            int i = 0;
            byte[] buffer = new byte[1024 * 1024];
            int bytesRead;
            ByteArrayOutputStream output = null;
            while ((bytesRead = reader.read(buffer)) != -1)
            {
                output = new ByteArrayOutputStream();
                output.write(buffer, 0, bytesRead);

                builder.addTextBody("command", "APPEND", ContentType.TEXT_PLAIN);
                builder.addTextBody("media_id", mediaId, ContentType.TEXT_PLAIN);
                builder.addTextBody("segment_index", String.valueOf(i), ContentType.TEXT_PLAIN);

                builder.addTextBody("media_data", BASE64Encoder.encode(output.toByteArray()), ContentType.TEXT_PLAIN);

                multipart = builder.build();
                uploadFile.setEntity(multipart);

                consumer.sign(uploadFile);

                response = httpClient.execute(uploadFile);
                responseEntity = response.getEntity();

                code = response.getStatusLine().getStatusCode();

                i++;

            }

            reader.close();

        }

    }
    catch (Exception e)
    {
        log.error("", e);
    }

    return code;
}

Logs.

org.apache.http.entity.ContentType.create(Ljava/lang/String;[Lorg/apache/http/NameValuePair;)Lorg/apache/http/entity/ContentType;] at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1305) at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68) at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117) at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108) at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369) at ognl.ASTMethod.getValueBody(ASTMethod.java:90) at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) at ognl.SimpleNode.getValue(SimpleNode.java:258) at ognl.Ognl.getValue(Ognl.java:494) at ognl.Ognl.getValue(Ognl.java:458) at com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:309) at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340) at com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:307) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:423) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:287) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:250) at com.inrev.bm.interceptor.IRPermissionInterceptor.intercept(IRPermissionInterceptor.java:57) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.inrev.bm.interceptor.IRAppAccessInterceptor.intercept(IRAppAccessInterceptor.java:49) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.inrev.bm.interceptor.IRLoggingInterceptor.intercept(IRLoggingInterceptor.java:555) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.s360.xssfilter.XSSFilter.doFilter(XSSFilter.java:94) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NoSuchMethodError: org.apache.http.entity.ContentType.create(Ljava/lang/String;[Lorg/apache/http/NameValuePair;)Lorg/apache/http/entity/ContentType; at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:219) at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:240) at com.inrev.bm.util.impl.IRTwitterUtilImpl.uploadMediaAppend(IRTwitterUtilImpl.java:2184) at com.inrev.bm.util.impl.IRTwitterUtilImpl.updateVideoStatus(IRTwitterUtilImpl.java:2333) at com.inrev.social.action.IRSocialMessageAction.sendMsgToTwitter(IRSocialMessageAction.java:5135) at com.inrev.social.action.IRSocialMessageAction.getSchMsgAccounts(IRSocialMessageAction.java:4239) at com.inrev.social.action.IRSocialMessageAction.add(IRSocialMessageAction.java:915) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870) at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293) ... 82 more

Post request from Java-Application on HCP isn´t performed

I got a problem with a httpPost from a Java-Application which is running on HCP-Trial-Account in a Tomcat7-Container. I use HttpClient 4.5.3.

The code runs on my local Tomcat7-Server fine and works. However, if deploy it to the HCP there occurs a problem.

public static Notebook getAllNotebooks(String code, String redirectUri) throws IOException, URISyntaxException{
        ClassLoader classLoader = Connection.class.getClassLoader();
        URL resource = classLoader.getResource("org/apache/http/impl/client/HttpClientBuilder.class");
        String returnUri = "https://login.live.com/oauth20_token.srf";
        HttpPost tokenRequest = new HttpPost(returnUri);
        HttpClient client = new DefaultHttpClient();//HttpClientBuilder.create().build(); //Exception: http://stackoverflow.com/questions/22330848/httpclient-example-exception-in-thread-main-java-lang-nosuchfielderror-inst

        tokenRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
        tokenRequest.setEntity(new UrlEncodedFormEntity(Connection.getParametersForURLBody(code, redirectUri), Consts.UTF_8));
        tokenRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
        HttpResponse tokenResponse = client.execute(tokenRequest); //Here it gets stuck

The problem occurs when the tokenRequest is executed. The application gets stuck and the tokenRequest runs forever.

Here are the threads that are running forever which I can see in debug mode:

SAP JVM Debug Target    
Daemon Thread [NioBlockingSelector.BlockPoller-1] (Running) 
Daemon Thread [NioBlockingSelector.BlockPoller-2] (Running) 
Daemon Thread [RMI TCP Connection(1)-127.0.0.1] (Running)   
Thread [Timer-0] (Running)  
Daemon Thread [RMI TCP Connection(2)-10.117.35.76] (Running)    
Thread [pool-1-thread-1] (Running)  
Thread [Timer-1] (Running)  
Daemon Thread [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (Running)    
Daemon Thread [JCoTimeoutChecker] (Running) 
Daemon Thread [http-bio-8001-Acceptor-0] (Running)  
Daemon Thread [http-bio-8001-AsyncTimeout] (Running)    
Daemon Thread [http-nio-127.0.0.1-9001-ClientPoller-0] (Running)    
Daemon Thread [http-nio-127.0.0.1-9001-Acceptor-0] (Running)    
Daemon Thread [http-nio-8041-ClientPoller-0] (Running)  
Daemon Thread [http-nio-8041-Acceptor-0] (Running)  
Daemon Thread [ajp-bio-8009-Acceptor-0] (Running)   
Daemon Thread [ajp-bio-8009-AsyncTimeout] (Running) 
Daemon Thread [RMI TCP Connection(10)-127.0.0.1] (Running)  
Daemon Thread [RMI TCP Connection(11)-10.117.10.34] (Running)   
Thread [main] (Running) 
Daemon Thread [http-nio-8041-exec-1] (Running)  
Daemon Thread [http-nio-8041-exec-2] (Running)  
Daemon Thread [http-nio-8041-exec-3] (Running)  
Daemon Thread [http-nio-8041-exec-4] (Running)  
Daemon Thread [http-nio-8041-exec-5] (Running)  
Thread [pool-2-thread-1] (Running)  
Daemon Thread [http-nio-8041-exec-6] (Stepping) 
Daemon Thread [http-nio-8041-exec-7] (Running)  
Daemon Thread [http-nio-8041-exec-8] (Running)  
Daemon Thread [http-nio-8041-exec-9] (Running)  
Daemon Thread [http-nio-8041-exec-10] (Running) 
Daemon Thread [RMI TCP Connection(idle)] (Running)  
Daemon Thread [RMI TCP Connection(idle)] (Running)  

The program gets stucked at this point.

I have no clue what to do and would really appreciate some hints and help :).

Greetings Maverin

One Note Api rejects Bearer Token, Error 401

i got a problem with sending bearer token to the One Note API.

String returnUri = "https://login.live.com/oauth20_token.srf";
HttpClient client = HttpClientBuilder.create().build();
HttpPost tokenRequest = new HttpPost(returnUri);
tokenRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
tokenRequest.setEntity(new UrlEncodedFormEntity(Connection.getParametersForURLBody(), Consts.UTF_8));
tokenRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
HttpResponse tokenResponse = client.execute(tokenRequest);
HttpGet getTopFiveNotebooks = new HttpGet("https://www.onenote.com/api/v1.0/me/notes/notebooks?top=5");

getTopFiveNotebooks.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + Connection.getValueByKey("access_token", Connection.getTokenInJson(tokenResponse)));

I got the Bearer Token and the header of the HttpGet-Request looks like this, if I look at it in debug-mode:

Token in the HtppGet-Header

But when I try to perform the get, the API gives me a 401 Unauthorized Error.

My Scope is scope=wl.basic+onedrive.readwrite, so the token should have all permissions it needs.

Update: If I login into https://apigee.com/onenote/embed/console/onenote/ with my microsoft-account and copy the access-token from there into this peace of code:

getTopFiveNotebooks.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + acces-key-from-the apigee-console)

it performs the get and give me Status 200 back instead of 401.

So my scope is wrong?

I would really appreciate help, because I don´t know how to do it?

Greetings Maverin

Edit: My Scope was false.

How to use Default System proxy using Apache HttpClient

I have proxy set in my system settings. I want to send http request through this proxy using Apache HttpClient 4.5.2

According to the Documentation, it says

One can also instruct HttpClient to use the standard JRE proxy selector to obtain proxy information

SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(
        ProxySelector.getDefault());
CloseableHttpClient httpclient = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();

But this is not working. If I use

HttpHost proxy = new HttpHost("someproxy", 8080); //-->Set Proxy manually
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);

it works. But I don't want to set proxy manually. I want the ProxySelector to detect any proxy

Here my full code

SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());

        CloseableHttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();
        try {
            HttpHost target = new HttpHost("google.co.in", 443, "https");

            HttpGet request = new HttpGet("/");

            System.out.println("Executing request " + request.getRequestLine() + " to " + target);

            CloseableHttpResponse response = httpclient.execute(target,request);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }

Microservice architecture | Cannot assign requested address

We have a microservice architecture where a single request from public (mobile app) internally results into 4 HTTP calls to different services.

One of the side affects of this I have observed is that, under heavy load, we start experiencing errors "Cannot assign requested address".

Running netstat -tunp | wc -l shows count of around 60k while normally it hovers around 3k.

It seems that I am running into port exhaustion. My application is written in Java using Apache HTTP Client.

What is the best way to solve this? I have few solutions in mind, but I am not sure if its the right way to go:

Any ideas?

Target host is not specified with apache component

I use spring boot I try to do a call to a web service.

My client localhost:9000 try to call localhost:8080

final RequestConfig config = RequestConfig.custom().setConnectTimeout(1000 * 1000).setConnectionRequestTimeout(1000 * 1000).setSocketTimeout(1000 * 1000).build();


final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("http://localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("joe", "sixpack"));
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).setDefaultCredentialsProvider(credentialsProvider).build();

final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
RestTemplate restTemplate2 = new RestTemplate(requestFactory);


ResponseEntity responseEntity = restTemplate2.patchForObject("/rest/members/{memberId}/card?cardId=", null, ResponseEntity.class, memberId, cardId);

When i do the call

org.apache.http.ProtocolException: Target host is not specified