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

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

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

    Believe it,do it!

    Ideal is the beacon. Without ideal, there is no secure direction; without direction ,there is no life.
    理想是指路明燈。沒有理想,就沒有堅(jiān)定的方向;沒有方向,就沒有生活。
    CTRL+T eclipse
    posts - 35, comments - 3, trackbacks - 0, articles - 0
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    用JAVA調(diào)用.net的webservice實(shí)例

    Posted on 2009-05-18 13:57 三羽 閱讀(2120) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA資料
     首先登錄www.wopos.com/webservice/Weather.asmx?op=getWeather。可以看到如下的SOAP信息 
    請(qǐng)求:
    以下內(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ù)制下來(lái)創(chuàng)建一個(gè)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="h ttp://www.w3.org/2001/XMLSchema" xmlns:soap="h ttp://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <getWeatherResponse xmlns="h ttp://tempuri.org/">
          <getWeatherResult>string</getWeatherResult>
        </getWeatherResponse>
      </soap:Body>
    </soap:Envelope>

    在后面對(duì)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替換原來(lái)的${city}$
            } 
            catch (Exception ex) 
            {
                ex.printStackTrace();
                return null;
            }
        }
      /*
        *返回InputStream是因?yàn)閣3c DOM中Document的parse方法可
        *以接受InputStream類型的參數(shù),方面在下一步對(duì)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對(duì)返回的XML進(jìn)行解釋
      *
      */
        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;
            }
        }
    }

    寫個(gè)main方法檢驗(yàn)一下結(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℃,風(fēng)==微風(fēng),紫外線==弱
    主站蜘蛛池模板: 色噜噜亚洲精品中文字幕| 日本一区二区三区免费高清| 成人伊人亚洲人综合网站222| 水蜜桃亚洲一二三四在线| 精品国产免费人成网站| 久久伊人亚洲AV无码网站| 日韩在线一区二区三区免费视频| 免费夜色污私人影院在线观看| 午夜亚洲国产精品福利| 亚洲国产黄在线观看| 一个人看的免费观看日本视频www| 在线观看亚洲专区| 免费日韩在线视频| 一级毛片完整版免费播放一区| 久久久精品国产亚洲成人满18免费网站 | 亚洲精品无码久久毛片波多野吉衣| 亚洲网站免费观看| 亚洲色少妇熟女11p| 免费欧洲美女牲交视频| 91在线视频免费观看| 亚洲人成电影在线天堂| 黄色网页免费观看| 在线观看亚洲精品国产| 可以免费观看的国产视频| 亚洲成a人片77777群色| 日本久久久免费高清| 中文字幕免费在线看电影大全| 久久久久亚洲AV成人无码网站| 99久久免费精品高清特色大片| 久久精品国产亚洲AV蜜臀色欲 | 亚洲毛片基地4455ww| 国产精品免费播放| 最新久久免费视频| 亚洲国产精品久久人人爱| 国产又粗又长又硬免费视频| 国产高清视频免费在线观看| 亚洲精品电影在线| 免费在线不卡视频| 免费观看无遮挡www的小视频| 亚洲AⅤ男人的天堂在线观看| 国产精品国产亚洲精品看不卡|