Java Jersey 2客户端和代理

时间:2014-11-27 13:25:13

标签: java rest proxy jersey jersey-2.0

我使用Jersey 2.11访问confluence REST API并从那里获取内容。

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

...

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword"
);

ClientConfig cc = new ClientConfig();
cc.property(ClientProperties.PROXY_URI, "xx.xx.xx.xx:xxxx");
Client client = ClientBuilder.newClient(cc);

client.register(feature);

WebTarget target = client.target(credential.getUrl())
    .path("rest").path("api").path("content").path(id)
    .queryParam("expand", "space,body.storage,version,container,ancestors");

Response response = target.request().get();

这是目前在泽西岛2.11中使用Java代理的正确方法吗?我总是异常

java.net.ConnectException: Connection refused: connect
javax.ws.rs.ProcessingException: java.net.Connect Exception: Connection refused: connect
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:229)
...

我想访问的汇合网站使用https。在不需要代理的机器上,它可以与

一起使用
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword");
Client client = ClientBuilder.newClient();
client.register(feature);

所以Confluence不是问题所在。我认为代理参与是错误的,但不知道是什么。

2 个答案:

答案 0 :(得分:1)

我无法管理泽西客户端中的代理,因此我使用默认的客户端创建

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword");
Client client = ClientBuilder.newClient();
client.register(feature);

并使用

启动我的应用程序
java -Dhttps.proxyHost=xx.xx.xx.xx -Dhttps.proxyPort:xxxx -jar myApp.jar

如果有人找到正确的解决方案来处理这件衣服2.11随时发布答案:)

答案 1 :(得分:0)

请尝试以下代码。我使用Jersey 2.0.1,并且能够将代理设置设置为clientConfig对象。并且可以使用

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {


     output$distPlot <- renderPlot({
    malaria = input$malaria
    i = 'a'


     if(input$p=='a'){
      i<4
    }

    if(input$p=='b'){
      i<-5
    }


    postM    <-malaria[,4]

    bins <- seq(0, 100, length.out =1)
    hist(postM, breaks = bins, col = 'darkgray', border = 'white')
  })
})


For ui.R

   library(shiny)

    malaria <- read.csv("file:///C:/Users/Bella/Desktop/malaria/Malaria2/data/malaria.csv",header=FALSE)
year <- malaria$year

ui <- fluidPage(

  titlePanel("Rate Death of Malaria"),

     sidebarLayout(
    sidebarPanel(

      radioButtons("radio", label = h3("Select the sample of countries"),
                   choices = list("Bangladdesh", "China","Ethiopia","South Africa",
                                  "Indonesia","India", "Kenya","Cambodia"," Myanmmar","Malaysia","Nigeria","Somalia","Thailand"),
                   selected = "World"),

      selectInput("vertical","Show vertical line in year(s):", 
                  choices = unique(malaria$year),multiple=TRUE
      ),

      checkboxInput("hor", "Show horizontal axis", TRUE)

    ),

    mainPanel(

      plotOutput("lineChart")
    )   
  )      
)