如何在JAX-WS中添加HTTP代理?

问题描述 投票:0回答:1

我有一个WSDL文件,通过在SoapUI中使用WSDL2Java把它变成了Java代码,它工作得很好,但是现在我需要把我公司的代理添加到其中,这样每个SOAP http请求都会通过它(但不是其他http请求)。

我看了很多关于这个问题的帖子,发现了这些选项。

  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", "[email protected]");
    ((BindingProvider) port).getRequestContext().put("http.proxyPort", "80");
    

    在这里,我使用了一个随机的不存在的代理,并希望得到一个任何形式的错误(超时,无效的代理,等等),但它却没有任何错误。

java http proxy cxf jax-ws
1个回答
0
投票

下面是一个没有使用第三方库的例子。

https:/github.comschuchjaxws-proxy-exampleblobmasterjaxws-client-with-proxysrcmainjavachschuexamplehelloworldClient.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.comschuchjaxws-proxy-exampleblobmasterjaxws-client-with-proxysrcmainjavachschuexamplehelloworldMyProxySelector.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.");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.