//这个方法的作用其实是构造你要向.net的Webservice发送的soap请求
private String getSOAPRequest(String request){
   StringBuffer buffer=new StringBuffer();
   buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
   buffer.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
   buffer.append("<soap:Body>");
   buffer.append("<Transaction xmlns=\"http://www.mrf.com/svg/webservice/WKTService\">");
   buffer.append("<strRequest>");
   buffer.append("<![CDATA[");
   buffer.append(request);
   buffer.append("]]>");
   buffer.append("</strRequest>");   buffer.append("</Transaction>");
   buffer.append("</soap:Body>");
   buffer.append("</soap:Envelope>\r\n");
   return buffer.toString();
 }//调用webservice的方法
public WFS_TransactionResponse executeQuery(String request){
    try{      String soap = getSOAPRequest(request);
//你要调用的.net的Web service的地址
      URL url=new URL(m_urls+"WKTService.asmx");
      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");
//你要调用的Web service的Web方法为Transaction
      conn.setRequestProperty("SOAPAction", "\"http://www.mrf.com/svg/webservice/WKTService/Transaction\"");      OutputStream os=conn.getOutputStream();
      OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
//      os.write(getPostRequest(request).getBytes());
      //发送请求
osw.write(soap);
      osw.flush();
      osw.close();      //接受web service的response
InputStream is = conn.getInputStream();
      InputStreamReader isr = new InputStreamReader(is,"utf-8");
//     String t = this.getPostResponse(is);
      String t = this.getSOAPResponse(isr);
      is.close();
      if(t == null)
        throw new Exception("Transaction Failed");
          
      return response;
    }
    catch (IOException ioe){
      ioe.printStackTrace();
      JOptionPane.showMessageDialog(null, "Connection failed.", "Connection failed", JOptionPane.ERROR_MESSAGE);
      return null;
    }
    catch(Exception ex){
      ErrorHandler err = new ErrorHandler(this.getClass().getName(), ex);
      err.ShowException("Transaction failed");
      return (null);
    }
  }
这是在Applet中调用.net的Web service的代码,在Tomcat中应该也一样

解决方案 »

  1.   

    用wsdl2java把服务端提供的wsdl编程一堆java源码再调用
      

  2.   

    IDE中有对应的支持的,你在Eclipse中建一个webservice的项目,并将服务方提供的wsdl导入到项目中,就可以通过IDE生成java源代码呀,并且可以直接生成Testcase. 在Testcase中调用成功后,再在tomcat中的servlet中调用javabean就行了.你看看你的IDE的帮助吧.
      

  3.   

    /*
     * 这是一个调用天气预报webservice的例子,是我参考huataixiang19810225(无名) 的
     *回复写的。相关的SOAP可以在http://www.wopos.com/webservice/Weather.asmx
     *找到,并且把它复制到一个weathersoap.xml文件中(其它文本文件也可以)
     *关于w3c dom的操作请参考其它资料
     *希望对你有点帮助
     */package jaqcy.weatherreport.client;import java.io.*;
    import java.net.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    /**
     *
     * @author jaqcy
     */
    public class WeatherReport
    {    
        private static String getSoapRequest(String city)
        {
            try 
            {
                Class cls=Object.class;
                InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("/jaqcy/weatherreport/client/weathersoap.xml"));
                BufferedReader reader=new BufferedReader(isr);
                String soap="";
                String tmp;
                while((tmp=reader.readLine())!=null)
                {
                    soap+=tmp;
                }            
                reader.close();
                isr.close();
                return soap.replace("${city}$",city);
            } 
            catch (Exception ex) 
            {
                ex.printStackTrace();
                return null;
            }
        }
        private static InputStream getSoapInputStream(String city)throws Exception
        {
            try
            {
                String soap=getSoapRequest(city);
                if(soap==null)
                {
                    return null;
                }
                URL url=new URL("http://www.wopos.com/webservice/Weather.asmx");
                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/getWeather\"");            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;
            }
        }
        public static String getWeather(String city)
        {
            try
            {
                Document doc;
                DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
                dbf.setNamespaceAware(true);
                DocumentBuilder db=dbf.newDocumentBuilder();
                InputStream is=getSoapInputStream(city);
                doc=db.parse(is);
                NodeList nl=doc.getElementsByTagName("getWeatherResult");
                Node n=nl.item(0);
                String weather=n.getFirstChild().getNodeValue();
                is.close();
                return weather;
            }
            catch(Exception e)
            {
                e.printStackTrace();
                return null;
            }
        }
        public static void main(String[] args)throws Exception
        {
            System.out.println(getWeather("珠海"));
        }
    }
      

  4.   

    <?xml version="1.0" encoding="UTF-8"?>
    <-- 这个是weathersoap.xml的内容-->
    <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>
        <getWeather xmlns="http://tempuri.org/">
          <mCity>${city}$</mCity>
        </getWeather>
      </soap:Body>
    </soap:Envelope>
      

  5.   

    高手用了你的程序,出现了错误如下
    java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:61)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
    at com.WeatherReport.getSoapRequest(WeatherReport.java:18)
    at com.WeatherReport.getSoapInputStream(WeatherReport.java:40)
    at com.WeatherReport.getWeather(WeatherReport.java:78)
    at com.WeatherReport.main(WeatherReport.java:94)
    java.lang.IllegalArgumentException: InputStream cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:94)
    at com.WeatherReport.getWeather(WeatherReport.java:79)
    at com.WeatherReport.main(WeatherReport.java:94)
    null
    weathersoap.xml文件的位置G:\JavaCodeTest\setTest\src\com\weathersoap.xml
    我修改了一下可是问题依旧为什么呢?
    InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("G:\\JavaCodeTest\\setTest\\src\\com\\weathersoap.xml"));
      

  6.   

    InputStreamReader isr=new InputStreamReader(new FileInputStream("G:\\JavaCodeTest\\setTest\\src\\com\\weathersoap.xml"));
      

  7.   

    cls.getResourceAsStream("/jaqcy/weatherreport/client/weathersoap.xml")是相对于类所在路径的
      

  8.   

    继续弱弱的问,帅哥俺都试过了捏,如下,
      InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("weathersoap.xml"));
      InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("/weathersoap.xml"));都是编译通过运行错误同上weathersoap.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>
        <getWeather xmlns="http://tempuri.org/">
          <mCity>${city}$</mCity>
        </getWeather>
      </soap:Body>
    </soap:Envelope>
      

  9.   

    放到类的编译目录下,哎,惭愧惭愧G:\JavaCodeTest\setTest\classes搞定了,真弱,多谢帅哥
      

  10.   

    city是调用服务端方法时传入的参数吧?我想传入三个参数怎么办呢?