0

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

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)
4

0 回答 0