您的位置:首页 > 其它

rpc调用webservice

2014-12-22 15:28 176 查看
ws发布后rpc的几种调用方式。
导入的几个jar:
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.rpc.client.RPCServiceClient;
1、指明返回值类型的调用
public static void testxml() throws Exception{

QName qname = new QName(" http://services.idcmanage.police.paibo.com/","addHostRoomFromIdc ");

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"

+ "<Body Description=\"IpConfig\" Code=\"0\">\n"

+" <Item  ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\"  SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n"

+" <Item  ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\"  SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n"

+ "</Body>";

Object[] param = new Object[] { xml };

String result="";

RPCServiceClient client =null;

try {

client = new RPCServiceClient();

Options options =  new Options();

options.setAction("urn:addHostRoomFromIdc");

options.setTo(new EndpointReference(" http://192.168.1.151:9999/services/SendIdcManageService/?wsdl "));

client.setOptions(options);

Class[] classtmp = new Class []{String.class};//输出返回值内容

result = (String) ((Object[])client.invokeBlocking(qname,param, classtmp))[0];

System.out.println(result);

client.cleanupTransport();

} catch (AxisFault e) {

e.printStackTrace();

logger.error("********sendSMSofSx:",e);

}catch (Exception e) {

e.printStackTrace();

logger.error("********sendSMSofSx:",e);

}finally{

if(client!=null){

client.cleanupTransport();

}

}

}

2、无指定返回值类型的调用,此时发布的ws参数必须为默认的arg0
public static void testxml() throws Exception{

QName qname = new QName(" http://services.idcmanage.police.paibo.com/","addHostRoomFromIdc ");

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"

+ "<Body Description=\"IpConfig\" Code=\"0\">\n"

+" <Item  ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\"  SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n"

+" <Item  ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\"  SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n"

+ "</Body>";

Object[] param = new Object[] { xml };

String result="";

RPCServiceClient client =null;

try {

client = new RPCServiceClient();

Options options =  new Options();

options.setAction("urn:addHostRoomFromIdc");

options.setTo(new EndpointReference(" http://192.168.1.151:9999/services/SendIdcManageService/?wsdl "));

client.setOptions(options);

OMElement element = client.invokeBlocking(qname, param);

result = element.getFirstElement().getText();

System.out.println(result);

client.cleanupTransport();

} catch (AxisFault e) {

e.printStackTrace();

logger.error("********sendSMSofSx:",e);

}catch (Exception e) {

e.printStackTrace();

logger.error("********sendSMSofSx:",e);

}finally{

if(client!=null){

client.cleanupTransport();

}

}

}


3、上两种方式都无法适用自定义的ws方式。特别是自定义的方法参数名称,即适用@WebParam(name="xml")
public static void testxml() throws Exception{

String path = "D:\\idcxml\\"+"addHostRoomFromIdc"+".xml";

File file = new File(path);

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String xml = "";

String readline  = "";

while((readline=br.readLine())!=null){

xml = readline;

System.out.println(xml);

}

String result="";

RPCServiceClient client =null;

try {
//System.setProperty("javax.net.ssl.trustStore", "d:\\idcxml\\idcstore.keystore");
//如果是https类型的ws,此句话解开即可访问(d:\\idcxml\\idcstore.keystore是你发布的路径)
client = new RPCServiceClient();

Options options =  new Options();

options.setTo(new EndpointReference(" http://192.168.1.151:9999/services/SendIdcManageService/?wsdl "));

client.setOptions(options);

// 创建一个OMFactory,下面的namespace、方法与参数均需由它创建

OMFactory factory = OMAbstractFactory.getOMFactory();

// 创建命名空间

OMNamespace namespace = factory.createOMNamespace(" http://services.idcmanage.police.paibo.com/ ", "urn");

// 参数对数 ******重要*******有些是指定方法名的。增加之后可以自定义参数名称

OMElement nameElement = factory.createOMElement("arg0", null);

nameElement.addChild(factory.createOMText(nameElement, xml));

// 创建一个method对象

OMElement method = factory.createOMElement("addHostRoomFromIdc", namespace);

method.addChild(nameElement);

OMElement resultOM = client.sendReceive(method);

result = resultOM.getFirstElement().getText();

System.out.println(result);

client.cleanupTransport();

} catch (AxisFault e) {

e.printStackTrace();

logger.error("********sendSMSofSx:",e);

}catch (Exception e) {

e.printStackTrace();

logger.error("********sendSMSofSx:",e);

}finally{

if(client!=null){

client.cleanupTransport();

}

}

}

推荐适用第三种方式,ws的请求就可以轻松自定义(都是测试通过的,根据自己需要调整)

4、另外一种调用方式
package client;

import java.io.FileOutputStream;
import java.util.Date;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

/*
* 仅适用于小附件上传、下载,10M以下。
*/
public class BlobRPCClient2
{
public static void main(String[] args) throws Exception
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/BlobService");
options.setTo(targetEPR);

//=================测试文件上传==================================

String filePath = "f:\\head.jpg";
DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));

//设置入参(1、文件名,2、DataHandler)
Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", dataHandler};

//返回值类型
Class<?>[] classes = new Class<?>[]{ Boolean.class };

//指定要调用的方法名及WSDL文件的命名空间
QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile");

//执行文件上传
System.out.println(new Date()+" 文件上传开始");
Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
System.out.println(new Date()+" 文件上传结束,返回值="+returnValue);

//=================测试文件下载==================================

opAddEntry = new QName("http://ws.apache.org/axis2", "downloadFile");
opAddEntryArgs = new Object[]{};
classes = new Class<?>[]{ DataHandler.class };

System.out.println(new Date()+" 文件下载开始");
DataHandler returnHandler = (DataHandler) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];
FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下载的文件.jpg");
returnHandler.writeTo(fileOutPutStream);
fileOutPutStream.flush();
fileOutPutStream.close();
System.out.println(new Date()+" 文件下载完成");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: