//这个方法的作用其实是构造你要向.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中应该也一样
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中应该也一样
* 这是一个调用天气预报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("珠海"));
}
}
<-- 这个是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>
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"));
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>