java使用AXIS2调用asp.net的WebService

来源:互联网 发布:淘宝店铺侵权 编辑:程序博客网 时间:2024/05/22 05:02

要求:按对方的要求把一串XML传给对方,对方返回一串XML结果.
对方使用的环境为ASP.NET

下载AXIS2包:
http://archive.apache.org/dist/ws/axis2/1_3/axis2-1.3-bin.zip
 
解压到E:/axis2-1.3目录.    (目录按自己的系统更改)

在DOS下进入到E:/axis2-1.3/bin目录

根据wsdl文件描述,使用 wsdl2java.bat 工具生成调用代码

命令如下:
wsdl2java.bat -uri  http://xxx.xxx.xxx:xxx/Services.asmx?wsdl

生成的两个class文件(ServicesCallbackHandler.java,ServicesStub.java)在E:/axis2-1.3/bin/src目录下面, 拷贝到你的项目中进行编译. 编译前要把E:/axis2-1.3/lib目录下面的包拷贝到你的项目中


调用测试:
public class TestCollectShowInfo
{
 public static String getXml() //该XML格式按照对方要求而生成
 {
  StringBuffer buf = new StringBuffer();  
  buf.append("<?xml version='1.0' encoding='UTF-8'?>");
  buf.append("<root>");
  buf.append("<head>");
  buf.append("<requestid>11111</requestid>");
  buf.append("<username>11111</username> ");
  buf.append("<password>11111</password>");
  buf.append("</head>");
  buf.append("<body>");
  buf.append("<record productid=/"1002/" title=/"东莞医疗机构首现社会监督员/" url =/"www.baidu.com/" createtime =/"2008-11-12/" type=/"0/"/>");
  buf.append("<record productid=/"1002/" title=/"78岁老太手术成功彻底结束/" url =/"www.baidu.com/" createtime =/"2008-11-11/" type=/"1/" />");
  buf.append("</body>");
  buf.append("</root>");
  return buf.toString();
 }

 public static void main(String[] args)
 {
  try
  {
   String uri = "http://xxx.xxx.xxx:xxx/Services.asmx";
   ServicesStub stub = new ServicesStub(uri);
   CollectShowInfo info = new CollectShowInfo();
   info.setXmlstr(getXml());   // xmlstr是要传给WEBSERVICE的内容.本例子传的是一个XML串
   CollectShowInfoResponse response = stub.CollectShowInfo(info);
   System.out.println(response.getCollectShowInfoResult()); //输出返回的结果.
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
}

注意:
CollectShowInfo及CollectShowInfoResponse两个CLASS是ServicesStub.java中的内部类.

原创粉丝点击