从Java调用Secure .asmx Web服务

时间:2013-08-16 12:19:43

标签: java security ws-security

我有一个web服务,其网址是这样的:

http://pc212323/AdminService/HelloWorldService.asmx?wsdl

现在,当我从soapUI点击此服务时,它会在一个单独的弹出窗口中询问用户名,密码。 这是在soapUI加载请求结构之前完成的。 再次,我必须在SoapUI工具左侧的请求属性中传递用户名和密码。然后它给我输出。

现在我想通过java点击这个asmx服务但是我无法弄清楚如何通过传递用户名和密码来调用这个服务。

总之,我想通过java代码

复制soapUI的这种行为

期待您的回答。提前谢谢。

我使用以下代码调用上述服务:

import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;


public class HelloWorldSecurity {

                public static void main(String[] args) {
                                HelloWorldSecurity helloWorldSecurity = new HelloWorldSecurity ();
                                helloWorldSecurity.service_code();
                }

                public ServiceClient configuration()
                {              
                                                System.out.println("configuration");
                                ServiceClient stClient = null;
                                try{
                                                org.apache.axis2.context.ConfigurationContext configurationContext = org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(null,null);
                                                MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
                                                HttpConnectionManagerParams params = new HttpConnectionManagerParams();
                                                params.setDefaultMaxConnectionsPerHost(2);
                                                multiThreadedHttpConnectionManager.setParams(params);
                                                HttpClient httpClient = new       HttpClient(multiThreadedHttpConnectionManager);
                                                configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT,httpClient);
                                                stClient = new ServiceClient(configurationContext , null);
                                                Options options = new Options();
                                                EndpointReference targetEPR = new EndpointReference("http://pc212323/AdminService/HelloWorldService.asmx");
                                                options.setTo(targetEPR);
                                                options.setAction("http://tempuri.org/HelloWorld");
                                                options.setUserName("kevin");
                                                options.setPassword("password-1");
                                                stClient.setOptions(options);
                                }
                                catch(Exception e)
                                {
                                                e.printStackTrace();
                                }
                                return stClient;
                }

                public void service_code(){
                                OMFactory fac = OMAbstractFactory.getOMFactory();
                                OMNamespace ns = fac.createOMNamespace("http://tempuri.org", "ns1");
                                OMElement result = null;
                                OMElement PayloadElement = fac.createOMElement("HelloWorld", ns);
                                ServiceClient client = configuration();
                                try {
                                     result = client.sendReceive(PayloadElement);
                                } catch (AxisFault e) {
                                                e.printStackTrace();
                                }
                }

}

但我得到的错误是:

org.apache.axis2.AxisFault: Transport error: 401 Error: Unauthorized
    at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:310)
    at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:194)
    at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
    at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:555)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:531)
    at HelloWorldSecurity.service_code(HelloWorldSecurity.java:63)
    at HelloWorldSecurity.main(HelloWorldSecurity.java:22)

0 个答案:

没有答案