既然是 ajax,就直接取得「資料」的部份就好
去剖析「網頁 HTML」的部份
既麻煩又是吃力不討好
直接取得資料的方式如下述
URL:https://www.megabank.com.tw/api/services/ExchangeServices.aspx/CallAPI
方法:POST
參數:{"args":"{\"service_name\":\"ex_rate_new\",\"args\":{}}"}
回覆:JSON 格式資料
d: {"ex_rate_new":{"last_update":"2020/03/15 18:52:19","args":"","curr_names":[{"curr_code":"01","curr_name":"USD","curr_cname":"美元"},{"curr_code":"03","curr_name":"HKD","curr_cname":"港幣"},{"curr_code":"04","curr_name":"GBP","curr_cname":"英鎊"},{"curr_code":"05","curr_name":"AUD","curr_cname":"澳幣"},{"curr_code":"06","curr_name":"SGD","curr_cname":"新加坡幣"},{"curr_code":"07","curr_name":"CHF","curr_cname":"瑞士法郎"},{"curr_code":"08","curr_name":"JPY","curr_cname":"日圓"},{"curr_code":"09","curr_name":"CAD","curr_cname":"加拿大幣"},{"curr_code":"12","curr_name":"SEK","curr_cname":"瑞典幣"},{"curr_code":"13","curr_name":"KRW","curr_cname":"韓幣"},{"curr_code":"14","curr_name":"MYR","curr_cname":"馬來幣"},{"curr_code":"15","curr_name":"IDR","curr_cname":"印尼幣"},{"curr_code":"17","curr_name":"THB","curr_cname":"泰幣"},{"curr_code":"21","curr_name":"PHP","curr_cname":"菲律賓幣"},{"curr_code":"24","curr_name":"NZD","curr_cname":"紐西蘭幣"},{"curr_code":"27","curr_name":"ZAR","curr_cname":"南非幣"},{"curr_code":"28","curr_name":"MOP","curr_cname":"澳門幣"},{"curr_code":"37","curr_name":"EUR","curr_cname":"歐元"},{"curr_code":"38","curr_name":"VND","curr_cname":"越南幣"},{"curr_code":"39","curr_name":"CNY","curr_cname":"人民幣"}],"currs":[{"curr_code":"01","curr_name":"USD","curr_cname":"美元","rates":{"bidlistrate":"30.1400","cashbidlistrate":"29.8000","asklistrate":"30.2400","cashasklistrate":"30.4700"}},{"curr_code":"03","curr_name":"HKD","curr_cname":"港幣","rates":{"bidlistrate":"3.8590","cashbidlistrate":"3.7290","asklistrate":"3.9190","cashasklistrate":"3.9350"}},{"curr_code":"04","curr_name":"GBP","curr_cname":"英鎊","rates":{"bidlistrate":"36.8700","cashbidlistrate":"35.8400","asklistrate":"37.2700","cashasklistrate":"37.9800"}},{"curr_code":"08","curr_name":"JPY","curr_cname":"日圓","rates":{"bidlistrate":"0.2780","cashbidlistrate":"0.2708","asklistrate":"0.2821","cashasklistrate":"0.2835"}},{"curr_code":"05","curr_name":"AUD","curr_cname":"澳幣","rates":{"bidlistrate":"18.5600","cashbidlistrate":"18.2800","asklistrate":"18.8000","cashasklistrate":"19.0700"}},{"curr_code":"09","curr_name":"CAD","curr_cname":"加拿大幣","rates":{"bidlistrate":"21.7900","cashbidlistrate":"21.4100","asklistrate":"21.9900","cashasklistrate":"22.3200"}},{"curr_code":"06","curr_name":"SGD","curr_cname":"新加坡幣","rates":{"bidlistrate":"21.2600","cashbidlistrate":"20.7700","asklistrate":"21.4400","cashasklistrate":"21.6600"}},{"curr_code":"27","curr_name":"ZAR","curr_cname":"南非幣","rates":{"bidlistrate":"1.8100","cashbidlistrate":"","asklistrate":"1.9100","cashasklistrate":""}},{"curr_code":"12","curr_name":"SEK","curr_cname":"瑞典幣","rates":{"bidlistrate":"3.0600","cashbidlistrate":"","asklistrate":"3.1600","cashasklistrate":""}},{"curr_code":"07","curr_name":"CHF","curr_cname":"瑞士法郎","rates":{"bidlistrate":"31.7100","cashbidlistrate":"31.0000","asklistrate":"31.9100","cashasklistrate":"32.1900"}},{"curr_code":"17","curr_name":"THB","curr_cname":"泰幣","rates":{"bidlistrate":"0.9290","cashbidlistrate":"0.8190","asklistrate":"0.9710","cashasklistrate":"1.0090"}},{"curr_code":"24","curr_name":"NZD","curr_cname":"紐西蘭幣","rates":{"bidlistrate":"18.2100","cashbidlistrate":"17.8200","asklistrate":"18.4100","cashasklistrate":"18.6900"}},{"curr_code":"37","curr_name":"EUR","curr_cname":"歐元","rates":{"bidlistrate":"33.3500","cashbidlistrate":"32.7300","asklistrate":"33.7500","cashasklistrate":"34.0700"}},{"curr_code":"13","curr_name":"KRW","curr_cname":"韓幣","rates":{"bidlistrate":"","cashbidlistrate":"0.0232","asklistrate":"","cashasklistrate":"0.0269"}},{"curr_code":"14","curr_name":"MYR","curr_cname":"馬來幣","rates":{"bidlistrate":"","cashbidlistrate":"5.9990","asklistrate":"","cashasklistrate":"7.5480"}},{"curr_code":"15","curr_name":"IDR","curr_cname":"印尼幣","rates":{"bidlistrate":"","cashbidlistrate":"0.0017","asklistrate":"","cashasklistrate":"0.0024"}},{"curr_code":"21","curr_name":"PHP","curr_cname":"菲律賓幣","rates":{"bidlistrate":"","cashbidlistrate":"0.5500","asklistrate":"","cashasklistrate":"0.6630"}},{"curr_code":"28","curr_name":"MOP","curr_cname":"澳門幣","rates":{"bidlistrate":"","cashbidlistrate":"3.5080","asklistrate":"","cashasklistrate":"3.9240"}},{"curr_code":"38","curr_name":"VND","curr_cname":"越南幣","rates":{"bidlistrate":"","cashbidlistrate":"0.0011","asklistrate":"","cashasklistrate":"0.0015"}},{"curr_code":"39","curr_name":"CNY","curr_cname":"人民幣","rates":{"bidlistrate":"4.2750","cashbidlistrate":"4.2050","asklistrate":"4.3250","cashasklistrate":"4.3670"}}]}}
各位高手們好:
目前測試預到1個問題,抓不出跟海綿寶寶一樣的資料,程式到using (WebResponse response = request.GetResponse()),這行就出錯了,不知問題出在哪?,請各位高手們指點一下小弟,感激不盡~ 謝謝。
試試看用 HTTPClient
2019 年的範例
海綿寶寶您好:
感謝您的指導~ 參考"2019 年的範例"程式後,我嘗試用HTTPClient做了個測試程式(如下),不過會出現StatusCode: 500, ReasonPhrase: 'Internal Server Error'...等訊息,不知JsonString是否設錯,請您再指點一下,感謝您~。
目前無法直接設成string JsonString = {"args":"{"service_name":"ex_rate_new","args":{}}"} 會出錯,所以改成 @"args"":{""service_name"":""ex_rate_new"",""args"":{}}"