<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 9, 文章 - 1, 評論 - 2, 引用 - 0
    數(shù)據(jù)加載中……

    [轉(zhuǎn)]用JAVA調(diào)用.net的webservice實例

     最近做的項目都是同webService有關(guān)的,自然就要關(guān)心一下webservice方面的資源。
    ::URL::http://www.wopos.com/webservice/Weather.asmx?op=getWeather

    是一個天氣預(yù)報的webservice,從它的輸出結(jié)果來看天氣數(shù)據(jù)應(yīng)該來自中央氣象局的問天網(wǎng)
    ::URL::http://www.tq121.com.cn/

    不過這方面就不用再多關(guān)心了,我們關(guān)心的是怎樣調(diào)用這個webservice。
           首先登錄www.wopos.com/webservice/Weather.asmx?op=getWeather。可以看到如下的SOAP信息 
    請求:
    以下內(nèi)容為程序代碼:

    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部分全部復(fù)制下來創(chuàng)建一個XML文件(普通的文本文件也可以),為了以后編程方便,把
    以下內(nèi)容為程序代碼:

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

    改成
    以下內(nèi)容為程序代碼:

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

    weathersoap.xml保存在以后生成的類的同一目錄。

    響應(yīng):
    以下內(nèi)容為程序代碼:

    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的解釋要用到響應(yīng)部分的XML描述


            接下就開始寫代碼了。
    以下內(nèi)容為程序代碼:

    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);//用傳入的參數(shù)city替換原來的${city}$
            }
     
            
    catch (Exception ex) 
            
    {
                ex.printStackTrace();
                
    return null;
            }

        }

      
    /*
        *返回InputStream是因為w3c DOM中Document的parse方法可
        *以接受InputStream類型的參數(shù),方面在下一步對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方法檢驗一下結(jié)果
    以下內(nèi)容為程序代碼:


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


    結(jié)果如下
    以下內(nèi)容為程序代碼:

    城市==珠海,日期==4.02-4.03,圖1==http://weather.tq121.com.cn/images/a1.gif,圖2==http://weather.tq121.com.cn/images/00.gif,天氣==多云,溫度==28℃~22℃,風==微風,紫外線==弱

    結(jié)果是有點亂,不過只要把它分割開來也是很容易的事。
            在自己的應(yīng)用程序中加上天氣預(yù)報的功能,對程序的競爭力也是有好處的,而且實現(xiàn)也相當?shù)暮唵危螛范粸槟兀?br>

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

    posted on 2007-04-06 09:08 趙貴陽 閱讀(6902) 評論(1)  編輯  收藏 所屬分類: WEBSERVICE

    評論

    # re: [轉(zhuǎn)]用JAVA調(diào)用.net的webservice實例   回復(fù)  更多評論   

    第三方撒范德薩發(fā)送到發(fā)送到
    2013-09-16 19:52 | 奧德賽

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 免费精品国产自产拍在线观看| 亚洲中文字幕无码永久在线| 久久久久亚洲精品影视| 亚洲AV网一区二区三区| 成年女人18级毛片毛片免费| 亚洲视频在线观看网站| 啦啦啦完整版免费视频在线观看 | 国产精品玖玖美女张开腿让男人桶爽免费看| 在线观看成人免费视频不卡| 一区二区三区亚洲| 精品一区二区三区无码免费视频| 国产亚洲av片在线观看16女人| 中文毛片无遮挡高清免费| 久久精品国产精品亚洲艾草网美妙| 污视频网站免费在线观看| 亚洲av日韩av无码黑人| 日韩高清在线免费看| 菠萝菠萝蜜在线免费视频| 亚洲女同成人AⅤ人片在线观看| 一个人看的免费观看日本视频www| 国产成人99久久亚洲综合精品 | 亚洲国产成人精品久久| 91成人免费观看网站| 又大又硬又粗又黄的视频免费看| 亚洲一区二区三区在线播放 | 国产午夜影视大全免费观看| 免费无码又爽又黄又刺激网站| 亚洲综合小说久久另类区| 成年男女免费视频网站| 男人j进入女人j内部免费网站| 精品日韩亚洲AV无码| 国产亚洲精久久久久久无码AV | 亚洲av无码国产精品夜色午夜| 91精品全国免费观看含羞草| 色多多www视频在线观看免费| 亚洲妓女综合网99| 成在线人永久免费视频播放| 亚洲毛片免费观看| 免费人成在线观看网站| 亚洲日本va一区二区三区| 亚洲一级片免费看|