[英]How to add HTTP proxy to JAX-WS?
I have a WSDL file which I've turned into Java code by using WSDL2Java inside SoapUI, it works fine, but now I need to add my company's proxy to it, so every SOAP http request would go through it (but not other http requests )。
我查看了有關同一問題的多個線程並找到了以下選項:
通過添加系統范圍的代理
System.getProperties().put("proxySet", "true"); System.getProperties().put("https.proxyHost", "10.10.10.10"); System.getProperties().put("https.proxyPort", "8080");
這對我不起作用,因為它會影響整個 jvm。
添加以下代碼
HelloService hello = new HelloService(); HelloPortType helloPort = cliente.getHelloPort(); org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloPort); HTTPConduit http = (HTTPConduit) client.getConduit(); http.getClient().setProxyServer("proxy"); http.getClient().setProxyServerPort(8080); http.getProxyAuthorization().setUserName("user proxy"); http.getProxyAuthorization().setPassword("password proxy");
我不知道如何使用。 我生成的代碼沒有任何org.apache.cxf的痕跡,只有javax.xml.ws 。
將此添加到我的端口配置中:
((BindingProvider) port).getRequestContext().put("http.proxyHost", "proxy@example.com"); ((BindingProvider) port).getRequestContext().put("http.proxyPort", "80");
在這里,我使用了一個隨機的不存在的代理,並期望得到任何類型的錯誤(超時、無效的代理等),但它卻沒有任何錯誤地通過。
這是一個不使用 3rd 方庫的示例。
package ch.schu.example.helloworld;
import java.net.ProxySelector;
import ch.schu.example.hello.HelloImpl;
import ch.schu.example.hello.HelloImplService;
public class Client {
public static void main(String[] args) {
ProxySelector.setDefault(new MyProxySelector());
HelloImplService service = new HelloImplService();
HelloImpl hello = service.getHelloImplPort();
System.out.println(hello.sayHello("Howard Wollowitz"));
}
}
package ch.schu.example.helloworld;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.*;
import java.util.*;
public class MyProxySelector extends ProxySelector {
@Override
public List<Proxy> select(URI uri)
{
System.out.println("select for " + uri.toString());
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 9999));
ArrayList<Proxy> list = new ArrayList<Proxy>();
list.add(proxy);
return list;
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
System.err.println("Connection to " + uri + " failed.");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.