java – 通过代理绕过请求不支持jaxws rs api
作者:互联网
我正在向外部流服务器发出http请求;因为我正在使用jaxws-rs-api.
我能够成功地提出请求并做出回应.但是当我尝试通过代理发出请求时,它不会通过代理(即使没有验证代理主机,如果我给了一些错误的IP也会拨打电话).
这是我试过的.
/**
*
*/
package com.rrr.test;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ChunkedInput;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
public class ClientCall {
private static final String BOUNDARY = "\r\n";
public void makeCall() {
//passing by passing apache connectors as an input to client config
// It is giving runtime error; versioning error, it is supportable for jdk8 it seems
//In my case, I am using JDK7
/*
* ClientConfig clientConfig = new ClientConfig();
* clientConfig.connectorProvider(new ApacheConnectorProvider());
* client.property(ClientProperties.PROXY_URI, "https://123.123.123.123");
*/
ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new HttpUrlConnectorProvider());
clientConfig.property(ClientProperties.PROXY_URI, "https://123.123.123.123:8080");
Client client = ClientBuilder.newClient().register(BasicAuthentication.class);
//passing as inline command line params
/*
* System.setProperty ("http.proxyHost", "10.1.224.60");
* System.setProperty ("http.proxyPort", "8080");
*/
WebTarget target = client.target("https://external/stream/v1/");
Builder request = target.request();
Response response = request.get();
System.out.println("Result " +response.getStatus());
final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
});
chunkedInput.setParser(ChunkedInput.createParser(BOUNDARY));
String chunk =null;
while ((chunk = chunkedInput.read()) != null) {
System.out.println(chunk);
}
}
public static void main(String[] args) {
ClientCall client = new ClientCall();
client.makeCall();
}
}
就我而言,我没有选择Java8的选项.如果任何库支持JDK7,请建议我.
我为此包含的依赖关系:
commons-collections-4-1,javax-servlet-api-2-5,log4j,slf4j-api-1-7-5,slf4j-log4j12-1-7-5,spring-framework-4-3-0,commons-logging-1-1,commons-configuration,commons-lang,jackson-core-asl-1-9-10,jackson-mapper-asl-1-9-10,spring-context-support-4-3-0,gson-2-8-2,httpcore-4-4-1,httpasyncclient-4-0-1,httpcore-nio-4-4-6,jackson-core-2-8-4,jackson-annotations-2-8-4,jackson-databind-2-8-4,httpclient-4-4-1,httpcore-4-4-1
解决方法:
ClientBuilder不使用变量clientConfig.
我建议你使用以下方法实例化你的JAXRS客户端:
Client client = ClientBuilder.newBuilder()
.withConfig(clientConfig)
.register(...)
.build();
标签:java,proxy,jax-rs,jersey-client,rest-client 来源: https://codeday.me/bug/20190710/1425389.html