调用webserver

1:调用带有用户名密码的服务端接口  使用 axis调用

***********************类开始*********************************

package com.test.common;

import java.util.ArrayList; import java.util.List; import javax.xml.
namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.engine.ListenerManager; import org.apache.axis2.rpc.client.RPCServiceClient; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HttpTransportProperties; import org.apache.axis2.transport.http.HttpTransportProperties.Authenticator; public class test { public Object test1() { ListenerManager.defaultConfigurationContext = null; Object result = ""; try { // 本机tomcat端口默认为8081,参数是wsdl网址的一部分 EndpointReference targetEPR = new EndpointReference( "http://127.0.0.1:8081/webService/testService?wsdl"); RPCServiceClient sender = new RPCServiceClient(); Options options = sender.getOptions(); options.setTimeOutInMilliSeconds(2 * 20000L);// 超时时间20s options.setTo(targetEPR); options.setAction("test");// 使用axis 调用cxf发布的webservice需要指定action , 调用 axis发布的服务端不需要 // 添加用户名和密码 HttpTransportProperties.Authenticator authenticator = new HttpTransportProperties.Authenticator(); List<String> auth = new ArrayList<String>(); auth.add(Authenticator.BASIC); authenticator.setAuthSchemes(auth); authenticator.setUsername("user");// 用户名 authenticator.setPassword("be601473-1a83-4f93-a94d-31b66a1bfa73");// 密码 options.setProperty(HTTPConstants.AUTHENTICATE, authenticator); /** * 参数: 1:在网页上执行 wsdl后xs:schema标签的targetNamespace路径 <xs:schema targetNamespace="http://test.axiswevservice.com">
* 2:<xs:element name="test"> ======这个标签中name的值
*/ QName qname = new QName("http://cewebservice.module.test.com/", "test"); String str = "客户端调用成功"; // 方法的入参 Object[] param = new Object[] { str }; Class<?>[] types = new Class[] { String.class }; // 这是针对返值类型的 /** * RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。 * invokeBlocking方法有三个参数 第一个参数的类型是QName对象,表示要调用的方法名; * 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; * 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。 * * 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。 */ Object[] response1 = sender.invokeBlocking(qname, param, types); System.out.println(response1[0]); result = response1[0]; } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }

****************************类结束*********************************

其他例子:

https://www.cnblogs.com/mracale/p/10578686.html

原文地址:https://www.cnblogs.com/xueershewang/p/15420715.html