簡體   English   中英

如何將 HTTP 代理添加到 JAX-WS?

[英]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 )。

我查看了有關同一問題的多個線程並找到了以下選項:

  1. 通過添加系統范圍的代理

    System.getProperties().put("proxySet", "true"); System.getProperties().put("https.proxyHost", "10.10.10.10"); System.getProperties().put("https.proxyPort", "8080");

    這對我不起作用,因為它會影響整個 jvm。

  2. 添加以下代碼

    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

  3. 將此添加到我的端口配置中:

     ((BindingProvider) port).getRequestContext().put("http.proxyHost", "proxy@example.com"); ((BindingProvider) port).getRequestContext().put("http.proxyPort", "80");

    在這里,我使用了一個隨機的不存在的代理,並期望得到任何類型的錯誤(超時、無效的代理等),但它卻沒有任何錯誤地通過。

這是一個不使用 3rd 方庫的示例。

https://github.com/schuch/jaxws-proxy-example/blob/master/jaxws-client-with-proxy/src/main/java/ch/schu/example/helloworld/Client.java

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"));
    }

}

https://github.com/schuch/jaxws-proxy-example/blob/master/jaxws-client-with-proxy/src/main/java/ch/schu/example/helloworld/MyProxySelector.java

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM