您好,登錄后才能下訂單哦!
今天碰巧,用到了淘寶的在線IP地址查詢的Rest API,它提供接口給用戶查詢IP地址的歸宿地。數(shù)據(jù)庫(kù)比較龐大,準(zhǔn)確性也比較高。地址為:http://ip.taobao.com/instructions.php。
這是一個(gè)GET請(qǐng)求的接口,使用方式很簡(jiǎn)單,只要將這個(gè)URL復(fù)制到瀏覽器的地址欄就可以了。
可以看到接口返回的是一串JSON格式的字符串。關(guān)于如何解析JSON,參考我的這篇博文:http://blog.csdn.net/chinacsharper/article/details/9246627。
那么如何在程序中實(shí)現(xiàn)呢?我們新建一個(gè)控制臺(tái)應(yīng)用程序,并鍵入如下代碼:
namespace RestServiceApp { class Program { static void Main(string[] args) { string url = "http://ip.taobao.com/service/getIpInfo.php?ip=210.75.225.254"; HttpWebRequest request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); Console.WriteLine(reader.ReadToEnd()); } } }
運(yùn)行它可以看到:
關(guān)于如何解碼\u4e2d\u56fd這樣的字符串,可以參考我的這篇博文:http://blog.csdn.net/chinacsharper/article/details/9885165。
好了,如何調(diào)用這個(gè)淘寶IP地址的接口就到這,不過我由此想到了兩個(gè)東西,一個(gè)是WCF Rest,一個(gè)是ASP.NET MVC中的Rest Api,這里先介紹一下WCF Rest。
我們?cè)谇拔囊埠?jiǎn)單的講解過如何使用WCF,WCF最關(guān)鍵的要素就是A(Address地址)、B(Binding綁定)、C(Contract契約)。為了便于演示,我們直接用控制臺(tái)應(yīng)用程序作為WCF Rest的宿主程序,先建立一個(gè)類庫(kù)項(xiàng)目,里面的代碼如下。
namespace Service.Interface { [ServiceContract] public interface IAddress { [WebGet(UriTemplate = "ip={ip}")] Address Get(string ip); } [DataContract] public class Address { [DataMember] public string IPAddress { get; set; } [DataMember] public string Province { get; set; } [DataMember] public string City { get; set; } } }
很簡(jiǎn)單,就是定義了一個(gè)契約,用于獲取IP地址信息。需要說明一下,這個(gè)項(xiàng)目需要引用System.ServiceModel和System.Runtime.Serialization這兩個(gè)dll。
接下來就是要?jiǎng)?chuàng)建一個(gè)WCF Rest服務(wù)端程序,用以提供服務(wù)。
先定義一個(gè)類AddressService,并實(shí)現(xiàn)我們剛剛定義的IAddress接口。
namespace Service { public class AddressService : IAddress { private static IList<Address> addresses = new List<Address>(); static AddressService() { //這里可以準(zhǔn)備一個(gè)IP地址庫(kù)并放入到IP地址列表中,以提供數(shù)據(jù)服務(wù) addresses.Add(new Address() { IPAddress = "210.75.225.254", Province = "上海市", City = "上海市" }); } public Address Get(string ipAddress) { return addresses.FirstOrDefault(x => x.IPAddress == ipAddress); } } }
接著在App.config配置文件中添加配置信息。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <services> <service name="Service.AddressService"> <endpoint address="http://127.0.0.1:8888/addresses" binding="webHttpBinding" contract="Service.Interface.IAddress"/> </service> </services> </system.serviceModel> </configuration>
最后在Main方法中添加如下代碼。
namespace Service { class Program { static void Main(string[] args) { using (WebServiceHost host = new WebServiceHost(typeof(AddressService))) { host.Open(); Console.Read(); } } } }
然后我們就可以運(yùn)行這個(gè)控制臺(tái)項(xiàng)目了。注意:運(yùn)行程序時(shí),請(qǐng)確保你當(dāng)前的用戶為操作系統(tǒng)管理員用戶。
服務(wù)端準(zhǔn)備好,我們寫一下客戶端的調(diào)用代碼,在控制臺(tái)應(yīng)用程序中測(cè)試一下這個(gè)WCF Rest服務(wù)。
同樣的,我們需要先在客戶端的App.config文件中配置一下這個(gè)服務(wù)(也可以在代碼中通過硬編碼的方式進(jìn)行)。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <client> <endpoint name="addressService" address="http://127.0.0.1:8888/addresses" binding="webHttpBinding" contract="Service.Interface.IAddress" /> </client> </system.serviceModel> </configuration>
這里要注意,綁定模型要跟服務(wù)端模型一致,否則會(huì)有綁定不匹配的異常產(chǎn)生。
客戶端代碼:
namespace RestServiceApp { class Program { static void Main(string[] args) { using (ChannelFactory<IAddress> channelFactory = new ChannelFactory<IAddress>("addressService")) { IAddress iAddress = channelFactory.CreateChannel(); Address address = iAddress.Get("210.75.225.254"); if (address != null) { Console.WriteLine(string.Format("IP來自{0},{1}",address.Province,address.City)); } } } } }
保證之前建立的服務(wù)端程序在運(yùn)行狀態(tài),然后我們運(yùn)行一下這個(gè)客戶端程序:
可以看到,我們調(diào)用WCF Rest服務(wù)成功的獲得了IP地址的詳細(xì)信息。
既然是Get請(qǐng)求型的Rest服務(wù),那么應(yīng)該可以在瀏覽器中直接調(diào)用,我們打開瀏覽器輸入地址。
同樣的獲得了XML類型的數(shù)據(jù)。
那如果我們想提供JSON格式的數(shù)據(jù)給別人呢?很簡(jiǎn)單,只要在定義接口方法時(shí),指定數(shù)據(jù)返回的格式即可。
[ServiceContract] public interface IAddress { [WebGet(UriTemplate = "ip={ip}", ResponseFormat = WebMessageFormat.Json)] Address Get(string ip); }
看到這里,你是否覺得Rest非常簡(jiǎn)單易用呢?
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。