fish2700 发表于 2013-1-15 02:48:25

Java中调用WebService

Java中调用WebService
今天被一个问题搞得挺郁闷得。整整弄了一天也没有得出个结果。项目中要调用一个webservice,
传入一个姓名,输出一串字符串。之前我也没有太多经验,网上找了些许资料,大概得参考了一下,自己
编写了几个类。大致如下:

package cn.sh.ideal.util;
import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;


public class RTXWebservice {
    private final static String ServiceURI="http://12.110.250.18/RTXMessageWebService/MessageRTX.asmx";
    public RTXWebservice() {
      try {
            jbInit();
      }
      catch (Exception ex) {
            ex.printStackTrace();
      }
    }
    private static String getSoapRequest(String name){//name为要查询的人名
      try
         {
             Class cls=Object.class;


             FileReader isr = new FileReader("../src/cn/sh/ideal/xml/rtxsoap.xml");
             BufferedReader reader=new BufferedReader(isr);
             String soap="";
             String tmp;
                         while((tmp=reader.readLine())!=null)
             {
               soap+=tmp;
               System.out.println("soap1:"+soap);
             }
             reader.close();
             isr.close();

             return soap.replaceAll("name",name);//用传入的参数name替换原来xml中的name
         }
         catch (Exception ex)
         {
             ex.printStackTrace();
             return null;
         }
    }
    /*
    *返回InputStream是因为w3c DOM中Document的parse方法可
    *以接受InputStream类型的参数,方面在下一步对XML的解释
    */
    private static InputStream getSoapInputStream(String name)throws Exception
    {
      try
      {
            String soap=getSoapRequest(name);

            if(soap==null)
            {
                return null;
            }
            URL url=new URL(ServiceURI);
            URLConnection conn=url.openConnection();
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestProperty("SOAPAction","\"http://tempuri.org/GetRTXSessionKey\"");
            OutputStream os=conn.getOutputStream();
            OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");
            osw.write(soap);
            osw.flush();
            osw.close();
            InputStream is=conn.getInputStream();
            return is;
      }
      catch(Exception e)
      {
            e.printStackTrace();
            return null;
      }
    }
    /*
   *用W3C DOM对返回的XML进行解释
   *@param name String 姓名
   *@return String rtxSessionKey
   */
    public static String getRTXSessionKey(String name)
    {
      try
      {
            Document doc;
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db=dbf.newDocumentBuilder();
            InputStream is=getSoapInputStream(name);
            doc=db.parse(is);
            NodeList nl=doc.getElementsByTagName("GetRTXSessionKeyResult");
            Node n=nl.item(0);
            String rtxsessionkey=n.getFirstChild().getNodeValue();
            is.close();
            return rtxsessionkey;
      }
      catch(Exception e)
      {
            e.printStackTrace();
            return null;
      }
    }
    private void jbInit() throws Exception {
    }
}

rtxsoap.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <GetRTXSessionKey xmlns="http://tempuri.org/">
      <strLoginID>name</strLoginID>
    </GetRTXSessionKey>
</soap:Body>
</soap:Envelope>


我先把webservice得wsdl文件找到
把request部分得xml粘贴下来保存在一个rtxsoap.xml得文件中并放到根目录+\src\cn\sh\ideal\xml\文件
夹下。我是要调http://12.110.250.18/RTXMessageWebService/MessageRTX.asmx(内网才能用)
的一个GetRTXSessionKey方法的。根据wsdl文件中的request和response的xml文件我写了上面的
方法。刚开始我用InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("../src/cn/sh/ideal/xml/rtxsoap.xml"))这种形式去找rtxsoap.xml文件作解析,想把参数弄进去,然后生
成个新的request的xml发给webservice来获取响应,可就是怎么也找不到那个rtxsoap.xml(它明明就是在那里
,可就是找不到,换了很多路径都是不对,估计问题还是处在路径上)后来换用 FileReader isr = new FileReader("../src/cn/sh/ideal/xml/rtxsoap.xml");方法。这下能够读到rtxsoap.xml了。
页: [1]
查看完整版本: Java中调用WebService