<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.
    理想是指路明燈。沒有理想,就沒有堅定的方向;沒有方向,就沒有生活。
    CTRL+T eclipse
    posts - 35, comments - 3, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    用JAVA調用.net的webservice實例

    Posted on 2009-05-18 13:57 三羽 閱讀(2109) 評論(0)  編輯  收藏 所屬分類: JAVA資料
     首先登錄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="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>

    在后面對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℃,風==微風,紫外線==弱
    主站蜘蛛池模板: 色婷婷六月亚洲综合香蕉| 亚洲欧洲第一a在线观看| 亚洲爆乳成av人在线视菜奈实| 18女人腿打开无遮掩免费| 亚洲午夜在线电影| 99免费在线观看视频| 亚洲一区二区三区日本久久九 | 亚洲小说图区综合在线| 91在线视频免费播放| 亚洲深深色噜噜狠狠网站| 欧美好看的免费电影在线观看| 久久亚洲精品专区蓝色区| 性xxxxx免费视频播放| 亚洲精品一二三区| 日韩一级视频免费观看| 美女黄频a美女大全免费皮| 亚洲无线一二三四区手机| 国产在线国偷精品免费看| 亚洲Av永久无码精品三区在线 | 67194熟妇在线永久免费观看 | 一区二区三区在线免费| 国产亚洲精品美女久久久| 18禁男女爽爽爽午夜网站免费| 亚洲AV日韩综合一区尤物| 亚洲国产综合无码一区二区二三区 | 亚洲AV无码一区二区三区网址| 亚洲国产91精品无码专区| 国产偷伦视频免费观看| 亚洲剧场午夜在线观看| 免费中文字幕在线观看| 免费国产成人18在线观看| 亚洲综合久久一本伊伊区| 亚洲国产日韩在线观频| 24小时在线免费视频| 18禁亚洲深夜福利人口| 亚洲成AV人片在WWW色猫咪| 成人免费无码大片A毛片抽搐色欲| 无遮挡a级毛片免费看| 亚洲视频一区在线| 免费在线观看的黄色网址| 毛片无码免费无码播放|