使用不同框架调用WebService
来源:互联网 发布:知乎回答如何删除 编辑:程序博客网 时间:2024/05/03 21:00
一、使用Axis调用WebService
package client;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**
* 使用Axis1框架调用WebService接口
* @author Administrator
*
*/
public class TestAxis1Client {
public static void main(String[] args) throws Exception {
t1();
}
private static void t1() throws Exception {
String url =
"http://135.33.9.43:9080/mhpools/services/UrgedRepairManager";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName("getRepairedPersons");
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root>"+
"<data>"+
"<KM_HURRY_SHEET>"+
"<ASK_ID>YH-6334-20110908-4580816</ASK_ID>"+
"<HURRY_NUM>30</HURRY_NUM>"+
"<HURRY_GRADE>3</HURRY_GRADE>"+
"</KM_HURRY_SHEET>"+
"</data>"+
"</root>";
String retStr = (String) call.invoke(new Object[] {str});
System.out.println(retStr);
}/*Method t1() end*/
}/*Class Test end*/
二、使用Axis2框架调用WebService
(1)
package client;
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;
public class TestAxis2Client2 {
public static void main(String[] args) {
String url = "http://135.33.9.43:9080/mhpools/services/UrgedRepairManager";
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root>"+
"<data>"+
"<KM_HURRY_SHEET>"+
"<ASK_ID>YH-6334-20110908-4580816</ASK_ID>"+
"<HURRY_NUM>30</HURRY_NUM>"+
"<HURRY_GRADE>3</HURRY_GRADE>"+
"</KM_HURRY_SHEET>"+
"</data>"+
"</root>";
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("", "");
OMElement method = fac.createOMElement("getRepairedPersons", omNs);
OMElement value = fac.createOMElement("Text", omNs);
value.addChild(fac.createOMText(value, str));
method.addChild(value);
//实例化Options对象
Options options = new Options();
//设置Options对象的操作事件对象
options.setAction("urn:getRepairedPersons");
//设置Options对象的连接终端地址,地址结尾没有?WSDL
options.setTo(new EndpointReference(url));
ServiceClient sender;
OMElement result = null;
try {
sender = new ServiceClient();
sender.setOptions(options);
result = sender.sendReceive(method);
} catch (AxisFault e) {
e.printStackTrace();
}
System.out.println(result.getFirstElement().getText());
}
}
(2)
package client;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
* 使用Axis2框架调用WebService接口
*
* @author Administrator
*
*/
public class TestAxis2Client {
public static void main(String[] args){
//webService的调用地址
String url = "http://135.33.9.43:9080/mhpools/services/UrgedRepairManager";
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root>"+
"<data>"+
"<KM_HURRY_SHEET>"+
"<ASK_ID>YH-6334-20110908-4580816</ASK_ID>"+
"<HURRY_NUM>30</HURRY_NUM>"+
"<HURRY_GRADE>3</HURRY_GRADE>"+
"</KM_HURRY_SHEET>"+
"</data>"+
"</root>";
String result = null;
//操作的命名空间+“:”+操作名 ,第一个参数可以为空
QName qname = new QName("", "getRepairedPersons");
//传递的参数对象集
Object param[] = new Object[] { str };
RPCServiceClient client = null;
OMElement element = null;
try {
//实例化远程服务调用客户端对象
client = new RPCServiceClient();
//实例化Options对象
Options options = new Options();
//设置Options对象的连接终端地址,地址结尾没有?WSDL
options.setTo(new EndpointReference(url));
//设置Options对象的操作事件对象
options.setAction("urn:getRepairedPersons");
//为远程服务调用客户端对象设置Options子对象
client.setOptions(options);
//传递参数,调用服务,获取服务返回结果集
element = client.invokeBlocking(qname, param);
} catch (AxisFault e) {
e.printStackTrace();
}
//获取返回结果集中第一条结果的返回内容
result = element.getFirstElement().getText();
System.out.println(result);
}
}
三、使用CXF框架调用WebService
package client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
* 使用CXF框架调用WebService接口
* @author Administrator
*
*/
public class TestCXFClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
t1();
}
public static void t1() throws Exception{
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory
.newInstance();
String wnmsUrl = "http://135.33.9.43:9080/mhpools/services/UrgedRepairManager?wsdl";
Client client = clientFactory.createClient(wnmsUrl);
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root>"+
"<data>"+
"<KM_HURRY_SHEET>"+
"<ASK_ID>YH-6334-20110908-4580816</ASK_ID>"+
"<HURRY_NUM>30</HURRY_NUM>"+
"<HURRY_GRADE>3</HURRY_GRADE>"+
"</KM_HURRY_SHEET>"+
"</data>"+
"</root>";
Object[] obj = client.invoke("getRepairedPersons", str);
String retStr = obj[0].toString();
System.out.println(retStr);
}
}
- 使用不同框架调用WebService
- C#不同方式调用webservice
- 不同方式调用webservice接口
- 使用JS调用WebService
- 使用Js调用WebService
- 使用Curl调用WebService
- 使用KSOAP2调用WebService
- 使用ajax调用webservice
- 使用URLConnection调用webservice
- 使用ASIHttpRequest调用WebService
- 使用ajax调用webservice
- 使用URLConnection调用webservice
- 使用ASIHttpRequest调用WebService
- 使用javascript调用webservice
- 使用urlConnection调用webservice
- 使用java调用webservice
- 使用axis调用webservice
- 使用goap调用webservice
- Android AsyncTask理解
- 解析短信
- 甘特图选型
- QueryPerformanceCounter 程序运行时间的开销探讨
- 第一页
- 使用不同框架调用WebService
- Oracle 数据流系统架构
- 实验6_图形用户界面高级组件及线程的编写
- mysql-bin.000001文件的来源及处理方法
- 分治算法求最近点对
- android 滑动显示item首字母
- 位运算取反、求绝对值
- Loadrunner使用过程中碰到的问题
- 局域网限速解决办法