如何在客户端中设置https Web服务的用户名和密码?

时间:2013-03-05 18:03:19

标签: web-services https cxf credentials

我使用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 Web服务的用户名和密码。但我可以在soapUI中运行此服务的测试用例,因为有一个选项可以设置端点的用户名和密码。如何在soapUI和apache-cxf-2.7.2创建的代码中设置它们?

1 个答案:

答案 0 :(得分:0)

您是否与SOAPUI绑定?如果没有,那么我会推荐Jersey Client API。这里有一个很好的例子,说明如何构建SOAP over REST客户端(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);