0

我使用soapUI 和apache-cxf-2.7.2 创建了https Web 服务的客户端代码。有一个名为 MYService_BasicEndpoint_Client.java 的类包含此方法:

public static void main(String args[]) throws java.lang.Exception {
    URL wsdlURL = MYServiceWcf.WSDL_LOCATION;
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) { 
        File wsdlFile = new File(args[0]);
        try {
            if (wsdlFile.exists()) {
                wsdlURL = wsdlFile.toURI().toURL();
            } else {
                wsdlURL = new URL(args[0]);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
}
    MYServiceWcf ss = new MYServiceWcf(wsdlURL, SERVICE_NAME);
    IMYService port = ss.getBasicEndpoint();
    port.webserviceMethod();
}

但是当我运行它时,结果是 http 响应 401(未经授权),因为没有任何选项可以设置 https 网络服务的用户名和密码。但我可以在soapUI 中运行此服务的测试用例,因为可以选择为端点设置用户名和密码。如何在上面由 soapUI 和 apache-cxf-2.7.2 创建的代码中设置它们?

4

1 回答 1

0

您是否与 SOAPUI 绑定?如果没有,那么我会推荐 Jersey Client API。这里有一个很好的例子,说明如何在 REST 客户端上构建 SOAP ( http://aruld.info/soap-over-resty-client/ )

这允许您使用 Apache HTTPClient 来设置您的请求(用户名、密码、端口和方案),然后创建 Jersey WebResource 最终将发送请求

示例配置可能类似于:

  DefaultHttpClient httpClient = connector.getHTTPClient(argUsername, argPassword, 
                    argScheme, argDomain);

  ClientConfig config = new DefaultClientConfig();
  config.getClasses().add(SoapProvider.class);
  HttpClientJerseyHandler clientHandler = new HttpClientJerseyHandler(httpClient, null, false);
  HttpClientJerseyClient jerseyClient = new HttpClientJerseyClient(clientHandler, config);
  return jerseyClient.resource(someWSDLURL);
于 2013-03-06T07:45:39.213 回答