【问题标题】:Post request from Java-Application on HCP isn´t performed未执行来自 Java 应用程序对 HCP 的发布请求
【发布时间】:2017-07-08 23:05:19
【问题描述】:

我在 Tomcat7-Container 中的 HCP-Trial-Account 上运行的 Java 应用程序的 httpPost 出现问题。我使用 HttpClient 4.5.3。

代码在我的本地 Tomcat7-Server 上运行良好并且工作正常。但是,如果将其部署到 HCP,则会出现问题。

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

执行tokenRequest时出现问题。应用程序卡住了,并且 tokenRequest 永远运行。

以下是我可以在调试模式下看到的永远运行的线程:

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)  

程序此时卡住了。

我不知道该怎么做,非常感谢一些提示和帮助:)。

问候 马弗林

【问题讨论】:

  • 很可能您需要配置代理。请参阅给定答案中引用的文档。
  • 没关系,但是例子都是UrlConnection,我不知道怎么用httpclient执行。非常感谢您提供更多帮助:)。
  • 您可以查看hc.apache.org/httpcomponents-client-ga/examples.html 以获取 apache http 客户端和代理的示例

标签: apache tomcat7 apache-httpclient-4.x onenote-api sap-cloud-platform


【解决方案1】:

我也遇到过这个问题,最后我找到了一个同样适用于 Tomcat8 的解决方案。关键是与构建器建立连接,然后使用 useSystemProperties()。通过这种方式,您可以获得 HCP 所需的代理属性。连接管理器的额外内容并不是严格需要的,但它有助于提高性能。 param-syncro 是遗留的东西。

org.apache.http.impl.conn.PoolingHttpClientConnectionManager cm
public static HttpClient getHttpClient(){
    if(cm == null){
        synchronized(params){
            cm = new PoolingHttpClientConnectionManager();
            // Increase max total connection to 200
            cm.setMaxTotal(200);
            // Increase default max connection per route to 20
            cm.setDefaultMaxPerRoute(200);
        }
    }
    org.apache.http.impl.client.CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .useSystemProperties()
            .build();
    return httpClient;
}

【讨论】:

    【解决方案2】:

    通常,您应该使用连接服务从 HCP 部署的应用程序中访问 Internet 资源。我猜想直接尝试这样做会导致您的应用程序冻结/崩溃。也可能是我缺少的其他东西。

    您可以在官方文档中找到有关执行此操作的信息:help.sap.com。

    简而言之,您必须:

    • 创建指向给定站点的目标(在您的 HCP 帐户中或直接在 Java 应用程序中)。
    • 由于您使用 HTTPS,您很可能必须将远程主机的证书导入信任库 (docu link)。
    • 在 web.xml 中声明连接配置。
    • 使用 JNDI(即 InitialContext)在您的 java 代码中获取连接配置。然后可以使用此配置来获取指向远程资源的 URL。您可以使用此 URL 打开直接连接或将其传递给 HttpClient(URL 类具有 toURI 方法)。

    【讨论】:

    • 在 HCP Tomcat7 运行时中,连接服务主要是用于配置(即 URL、用户/密码等)的安全存储。所有的东西,如 http 客户端生成、信任存储配置等都由应用程序完成。
    • 我遵循了这个例子:help.hana.ondemand.com/help/… 但我不知道如何使用 httpclient 而不是 ur-connection 来做到这一点?
    • 问题是,我尝试了tomcat 7的所有示例,但我真的不知道如何使用httpclient而不是url-connection来做到这一点。
    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多