随笔 - 9, 文章 - 1, 评论 - 2, 引用 - 0
数据加载中……

[转]用JAVA调用.net的webservice实例

 最近做的项目都是同webService有关的,自然就要关心一下webservice方面的资源。
::URL::http://www.wopos.com/webservice/Weather.asmx?op=getWeather

是一个天气预报的webservice,从它的输出结果来看天气数据应该来自中央气象局的问天网
::URL::http://www.tq121.com.cn/

不过这方面就不用再多关心了,我们关心的是怎样调用这个webservice。
       首先登录www.wopos.com/webservice/Weather.asmx?op=getWeather。可以看到如下的SOAP信息 
请求:
以下内容为程序代码:

POST /webservice/Weather.asmx http/1.1
Host: www.wopos.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/getWeather"

<?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>string</mCity>
    
</getWeather>
  
</soap:Body>
</soap:Envelope>

把XML部分全部复制下来创建一个XML文件(普通的文本文件也可以),为了以后编程方便,把
以下内容为程序代码:

                       ...
      <mCity>string</mCity> 
                       ...

改成
以下内容为程序代码:

                      ...
      <mCity>${city}$</mCity> 
                       ...

weathersoap.xml保存在以后生成的类的同一目录。

响应:
以下内容为程序代码:

http/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    
<getWeatherResponse xmlns="http://tempuri.org/">
      
<getWeatherResult>string</getWeatherResult>
    
</getWeatherResponse>
  
</soap:Body>
</soap:Envelope>

在后面对XML的解释要用到响应部分的XML描述


        接下就开始写代码了。
以下内容为程序代码:

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)//city为要查询天气的城市名
    {
        
try 
        
{
            Class cls
=Object.class;
            InputStreamReader isr
=new InputStreamReader(cls.getResourceAsStream("/jaqcy/weatherreport/client/weathersoap.xml"));//读取存在weathersoap的SOAP信息
            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);//用传入的参数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.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;
        }

    }

/*
  *用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;
        }

    }

}


写个main方法检验一下结果
以下内容为程序代码:


   
 public static void main(String[] args)throws Exception
    
{
        System.out.println(WeatherReport.getWeather(
"珠海"));
    }


结果如下
以下内容为程序代码:

城市==珠海,日期==4.02-4.03,图1==http://weather.tq121.com.cn/images/a1.gif,图2==http://weather.tq121.com.cn/images/00.gif,天气==多云,温度==28℃~22℃,风==微风,紫外线==弱

结果是有点乱,不过只要把它分割开来也是很容易的事。
        在自己的应用程序中加上天气预报的功能,对程序的竞争力也是有好处的,而且实现也相当的简单,何乐而不为呢?

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=669923

posted on 2007-04-06 09:08 赵贵阳 阅读(6879) 评论(1)  编辑  收藏 所属分类: WEBSERVICE

评论

# re: [转]用JAVA调用.net的webservice实例   回复  更多评论   

第三方撒范德萨发送到发送到
2013-09-16 19:52 | 奥德赛

只有注册用户登录后才能发表评论。


网站导航: