通过代理连接到Web服务

时间:2014-03-20 22:20:35

标签: java web-services soap wsdl wsimport

我使用wsimport从wsdl文件创建了java代码。

我想使用代理连接到Web服务,我得到了这个代码(生成):

package siri.siriservices;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "SiriServices", targetNamespace = "http://someip/Siri/SiriServices/", wsdlLocation = "file:/C:/Users/Pavel/git/stop-scanner/StopScanner/SIRI%20-%20ISRAEL/siri_wsProducer%20-%20Israel.wsdl")
public class SiriServices
    extends Service
{

    private final static URL SIRISERVICES_WSDL_LOCATION;
    private final static WebServiceException SIRISERVICES_EXCEPTION;
    private final static QName SIRISERVICES_QNAME = new QName("http://someip/Siri/SiriServices/", "SiriServices");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("file:/C:/Users/Pavel/git/stop-scanner/StopScanner/SIRI%20-%20ISRAEL/siri_wsProducer%20-%20Israel.wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        SIRISERVICES_WSDL_LOCATION = url;
        SIRISERVICES_EXCEPTION = e;
    }

    public SiriServices() {
        super(__getWsdlLocation(), SIRISERVICES_QNAME);
    }

    public SiriServices(WebServiceFeature... features) {
        super(__getWsdlLocation(), SIRISERVICES_QNAME, features);
    }

    public SiriServices(URL wsdlLocation) {
        super(wsdlLocation, SIRISERVICES_QNAME);
    }

    public SiriServices(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, SIRISERVICES_QNAME, features);
    }

    public SiriServices(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public SiriServices(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return
     *     returns SOAPPort
     */
    @WebEndpoint(name = "SiriWSPort")
    public SOAPPort getSiriWSPort() {
        return super.getPort(new QName("http://someip/Siri/SiriServices/", "SiriWSPort"), SOAPPort.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns SOAPPort
     */
    @WebEndpoint(name = "SiriWSPort")
    public SOAPPort getSiriWSPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://someip/Siri/SiriServices/", "SiriWSPort"), SOAPPort.class, features);
    }

    private static URL __getWsdlLocation() {
        if (SIRISERVICES_EXCEPTION!= null) {
            throw SIRISERVICES_EXCEPTION;
        }
        return SIRISERVICES_WSDL_LOCATION;
    }

}

我可以选择配置代理将WebServiceFeature传递给getSiriWSPort构造函数,如注释中所述:

 /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns SOAPPort
     */
    @WebEndpoint(name = "SiriWSPort")
    public SOAPPort getSiriWSPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://someip/Siri/SiriServices/", "SiriWSPort"), SOAPPort.class, features);
    }

我找到了一些有关配置here的信息,但我找不到包含ClientProxyFeature的jar。

谢谢!

1 个答案:

答案 0 :(得分:1)

你应该看看CXF

我使用基于CXF的客户端进行http代理和证书身份验证,效果非常好。我手边没有IDE,但在你的情况下,我猜它看起来像这样:

SiriServices siri = new SiriServices();
SOAPPort port = siri.getSiriWSPort();
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
conduit.getClient().setProxyServer("...");
conduit.getClient().setProxyServerPort(...);