求助:收不到SOAP消息

ivyyuan1 2008-07-01 04:41:09
参考了一个调用天气预报的例子,但是运行之后收不到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;
}
}
}
...全文
168 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjs_lh 2008-11-04
  • 打赏
  • 举报
回复
2楼正解。
leiju 2008-11-02
  • 打赏
  • 举报
回复
不好意思,由于没有时间维护,就把这个服务关了!我写的这个服务是分析中央气象台的网站数据得到的,可以分析那里的数据自己获取,sorry
chyx413332087 2008-07-11
  • 打赏
  • 举报
回复
up
swort_177 2008-07-09
  • 打赏
  • 举报
回复
代码没问题
关键问题是那边服务提供商已经把这个服务给关闭或者改为收费了!
得到的结果如下:
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@14a9972
无权限访问,若要开通请Q 76445866 .System.NullReferenceException: 未将对象引用设置到对象的实例。
在 Weather.getWeather(String mCity)

67,537

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧