求助:收不到SOAP消息
参考了一个调用天气预报的例子,但是运行之后收不到SOAP消息,见下面红色的部分。发出去的SOAP消息应该没有问题,已经打印出来
<?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://260dns.cn">
<city>beijing</city>
</GetWeather>
</soap:Body></soap:Envelope>
大家帮忙看看,这是什么问题?
package client;
import java.io.*;
import java.net.*;
import java.util.Properties;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class WeatherReport
{
public static void main(String[] args)throws Exception
{
Properties prop = System.getProperties();
prop.put("http.proxyHost","139.24.236.123");
prop.put("http.proxyPort","8080");
System.out.println(WeatherReport.getWeather("beijing"));
}
private static String getSoapRequest(String city)//city为要查询天气的城市名
{
try
{
Class cls=Object.class;
InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("/client/weathersoap.xml"));//读取存在weathersoap的SOAP信息
BufferedReader reader=new BufferedReader(isr);
String soap="";
String tmp;
while((tmp=reader.readLine())!=null)
{
soap+=tmp;
}
System.out.println("read successfully");
System.out.println(soap);
reader.close();
isr.close();
System.out.println(soap.replace("${city}$",city));
return soap.replace("${city}$",city);//用传入的参数city替换原来的${city}$
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
}
/*
*返回InputStream是因为w3c DOM中Document的parse方法可
*以接受InputStream类型的参数,方面在下一步对XML的解释
*/
private static InputStream getSoapInputStream(String city)throws Exception
{
try
{
String soap=getSoapRequest(city);
if(soap==null)
{
return null;
}
URL url=new URL("http://www.260dns.cn/Services/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://260dns.cn/GetWeather\"");
OutputStream os=conn.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");
System.out.println(soap);
osw.write(soap);
osw.flush();
osw.close();
InputStream is=conn.getInputStream();
String temp1 = is.toString();
System.out.println(temp1); 这里打印出来的结果是sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1100d7a
return is;
}
catch(Exception e)
{
System.out.println("exception1");
e.printStackTrace();
return null;
}
}
/*
*用W3C DOM对返回的XML进行解释
*
*/
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;
}
}
}