我正在使用 Jersey 2.4.1 进行休息,并希望通过 HTTP 和 HTTPS 代理进行 GET 或 Post 调用。我做不到。我在互联网上搜索并找到了很多链接,但其中大部分现在已经过时了。一些帮助将非常有用,因为从 Jersey 1.X 到 2.X 有很多变化
这是我进行 GET 调用的代码(工作正常)。我想修改它以通过 HTTP 和 HTTPS 代理进行此调用。任何指示都会有所帮助。
javax.ws.rs.core.Response response = null;
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url); //url is string
response = target.request().header("Authorization", header).accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get();
最佳答案
尝试使用 ClientConfiguration 对象,设置您想要的任何属性,然后使用 ClientBuilder.withConfig(Configuration config) 设置配置。然后您可以使用 build() 方法构建它。 看看这个例子:
ClientConfig cc = new ClientConfig();
cc.property(ClientProperties.PROXY_URI, "8.8.8.8:80");
Client client = JerseyClientBuilder.withConfig(cc).build();
但这只适用于 http 代理。 要设置 https 代理,您必须像这样设置系统属性:
System.setProperty("http.proxyHost", "some.proxy");
System.setProperty("http.proxyPort", "3476");
System.setProperty("https.proxyHost", "some.https.proxy");
System.setProperty("https.proxyPort", "6235");
阅读this了解更多信息。
关于java - Jersey 2.0 通过代理获取 Post Call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846264/