2013-03-05 82 views
0

我已经使用soapUI和apache-cxf-2.7.2创建了https web服务的客户端代码。有一个名为MYService_BasicEndpoint_Client.java包含此方法的类:如何在客户端设置https Web服务的用户名和密码?

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创建的代码中设置它们?

回答

0

你绑定到SOAPUI吗?如果没有,那么我会推荐Jersey客户端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);