使用Heroku - Fixie,帖子无法达到目标

时间:2016-04-19 09:14:15

标签: java rest heroku https proxy

我正在使用另一个REST API服务制作REST API服务。

当我发布使用外部REST API服务的请求时,帖子无法到达目标。 目标主机需要静态IP地址和HTTP协议,因此我在Heroku上使用Fixie。

我在heroku上收到以下消息。

o.apache.http.impl.execchain.RetryExec   : I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {tls}->http://xxxxx.usefixie.com:80->https://my-target-host.com:443: The target server failed to respond
o.apache.http.impl.execchain.RetryExec   : Retrying request to {tls}->http://xxxxx.usefixie.com:80->https://my-target-host.com:443 org.apache.http.NoHttpResponseException: my-target-host.com:443 failed to respond

Currentry,主机不公开访问日志。

这些是我的Java代码。 代码-A

HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-target-host.com",443,"https");
post.setHeader("Content-Type", "application/json; charset=UTF-8");

try{
    URL proxyUrl = new URL(System.getenv("FIXIE_URL"));

    HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
    HttpRoutePlanner routePlanner = new HttpRoutePlanner() {

        @Override
        public HttpRoute determineRoute(HttpHost target, org.apache.http.HttpRequest request,
                org.apache.http.protocol.HttpContext context) throws HttpException {
            return new HttpRoute(target, null,  new HttpHost(proxyUrl.getHost(), proxyUrl.getPort()), true);
        }
    };
    try (CloseableHttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();) {

        try {
            post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
            CloseableHttpResponse res = httpclient.execute(targetHost,post);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}catch(Exception e){
    e.printStackTrace();
}

代码-B。

HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-target-host.com",443,"https");
post.setHeader("Content-Type", "application/json; charset=UTF-8");

try{
    URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
    String userInfo = proxyUrl.getUserInfo();
    String user = userInfo.substring(0, userInfo.indexOf(':'));
    String password = userInfo.substring(userInfo.indexOf(':') + 1);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(proxyUrl.getHost(),proxyUrl.getPort()),
            new UsernamePasswordCredentials(user,password));

    try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();) {

        HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        post.setConfig(config);

        String encodedAuth = Base64.getEncoder().encodeToString(userInfo.getBytes());
        post.setHeader("Proxy-Authorization", "Basic " + encodedAuth);

        try {
            post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
            CloseableHttpResponse res = httpclient.execute(targetHost,post);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}catch(Exception e){
    e.printStackTrace();
}

代码-C

HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-tareget-host.com",443,"https");

try{
    URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
    String userInfo = proxyUrl.getUserInfo();
    String user = userInfo.substring(0, userInfo.indexOf(':'));
    String password = userInfo.substring(userInfo.indexOf(':') + 1);

    DefaultHttpClient httpclient_ = new DefaultHttpClient();
    try {
      httpclient_.getCredentialsProvider().setCredentials(
            new AuthScope(proxyUrl.getHost(), proxyUrl.getPort()),
            new UsernamePasswordCredentials(user, password));
      HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
      httpclient_.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
      String encodedAuth = Base64.getEncoder().encodeToString(userInfo.getBytes());

      post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
      post.setHeader("Proxy-Authorization", "Basic " + encodedAuth);

      HttpResponse rsp = httpclient_.execute(targetHost, post);

    } finally {
        httpclient_.getConnectionManager().shutdown();
    }
}catch(Exception e){
    e.printStackTrace();
}

这些代码会导致相同的结果。 在相同的环境中,达到了这个ruby代码。

RestClient.proxy = ENV['FIXIE_URL'] if ENV['FIXIE_URL']
RestClient.post("https://my-target-host.com/action", CONTENT_JSON, {
  'Content-Type' => 'application/json; charset=UTF-8'
})

访问时发生了什么?我应该在Java上修改什么?

0 个答案:

没有答案