您的位置:首页 > 编程语言 > Java开发

详解 Web service Axis2 JAVA调用.net接口 返回String的xml格式数据

2013-09-27 15:11 751 查看
Axis2相关jar包地址下载 http://download.csdn.net/detail/xyh94233/6329671
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;

public class WebServiceAxisXml {

public String sendUrl(Connection con,String accessType){
String xml = "";
String beginDate = "";
String endDate = "";
final String org_method = "GetAdmOrgUnitByUpdateTime";
final String usr_method = "GetLimitedEmpProfileByUpdateTime";
//发送请求的url
final String org_url = "http://10.103.117.13:8055/WebService/AdmOrgUnitWSforHR.asmx";
final String usr_url = "http://10.103.117.13:8055/WebService/EmpWSforHR.asmx";
//命名空间 注意:此处为.net设置的名称,我们只要名称相同即可,文章末尾有截图展示.net接口中命名空间的位置以及名称;如果不设置该命名空间		//有可能报出Server did not recognize the value of HTTP Header SOAPAction 服务器无法识别的SOAPAction HTTP头的值
final String qName = "http://newsso.xxx.com/";
//接收参数的名称
String param_begin = "BeginDate";
String param_end = "EndDate";
String url = "";
String method = "";
if(accessType.equals("1")){
//org 获取的用户组信息
url = org_url;
method = org_method;
}else{
//user 用户信息
url = usr_url;
method = usr_method;
}

try {
//获取当前时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
beginDate = format.format(date);//+" 00:00:00";
endDate = format.format(date);//+ " 23:59:59";

//创建服务客户端.
Service service = new Service();

Call call = null;

// 通过service创建call对象
call = (Call) service.createCall();

// 设置service所在URL(连接点)
call.setTargetEndpointAddress(url);

/**
* 设置要调用的方法:setOperationName的参数是javax.xml.namespace.QName实例
* 创建javax.xml.namespace.QName实例,第一个参数是webservice的url
* 第二个对数是你调用的webservice方法名
*/
call.setOperationName(new QName("http://hr.xxx.com/", method));
//需要传递过去的参数[参数名称,参数类型,传入] 这里必须用 new QName()不然可能对方接收不到该参数
call.addParameter(new QName(qName,param_begin),XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName(qName,param_end), XMLType.XSD_STRING, ParameterMode.IN);

//设置返回的类型值 string
call.setReturnType(XMLType.XSD_STRING);
//设置可识别的http信息
call.setUseSOAPAction(true);
//访问.net命名空间名称+方法名称
String path = qName + method;
call.setSOAPActionURI(path);
//通过invoke方法获取返回值xml的字符串,注意 new Object[]这个数组如果你不需要传值过去,也需要在这里定义一个空值
xml = (String)call.invoke(new Object[]{beginDate,endDate});

} catch (Exception e) {
e.printStackTrace();
}

return xml;
}

}

命名空间:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: