天天看點

iPhone中調用WCF RESTFUL Service

    REST(Representational State Transfer)是一種輕量級的Web Service架構風格,其實作和操作明顯比SOAP和XML-RPC更為簡潔,可以完全通過HTTP協定實作,還可以利用緩存Cache來提高響應速度,性能、效率和易用性上都優于SOAP協定。使用WCF建立restful分格的服務是非常友善的。這篇文章,我通過一個demo來展示如何在iPhone中調用wcf restful service。

  建立一個wcf restful service。

1、建立一個資料交換實體類

<a></a>

///&lt;summary&gt;

/// User實體類

///&lt;/summary&gt;

[DataContract]

publicclass User

{

/// 使用者名

[DataMember(Order =0)]

publicstring Name { get; set; }

/// 生日

[DataMember(Order =1)]

publicstring DayOfbirth { get; set; }

}

2、服務契約:定義了三個方法,分别用來擷取使用者的XML格式、json格式以及建立使用者。

[ServiceContract]

publicinterface IService1

/// 建立使用者

/// 擷取使用者資訊,json格式

///&lt;param name="name"&gt;使用者名&lt;/param&gt;

[OperationContract]

[WebGet(

UriTemplate ="User/{name}",

ResponseFormat = WebMessageFormat.Json)]

User GetUser(string name);

/// 擷取使用者資訊,XML格式

///&lt;remarks&gt;

///&lt;/remarks&gt;

UriTemplate ="UserXML/{name}",

ResponseFormat = WebMessageFormat.Xml)]

User GetUserXML(string name);

/// 建立使用者資訊

///&lt;param name="dayOfbirth"&gt;生日&lt;/param&gt;

///&lt;returns&gt;&lt;/returns&gt;

[WebInvoke(UriTemplate ="User/{name}/{dayOfbirth}",

Method ="POST",

User CreateUser(string name, string dayOfbirth);

3、服務實作:這裡簡單處理。

publicclass Service1 : IService1

public User GetUser(string name)

returnnew User { Name = name, DayOfbirth =new DateTime(1986, 10, 23).ToString() };

public User GetUserXML(string name)

public User CreateUser(string name, string dayOfbirth)

returnnew User { Name = name, DayOfbirth = dayOfbirth };

4、對于vs2008建立的wcf服務,需要在.svc檔案中加入下面代碼:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

5、在web.config中将綁定方式改成webHttpBinding

6、在iis釋出服務。

iPhone用戶端調用:

    這裡使用ASIHTTPRequest,它是一個直接在CFNetwork上做的開源項目,提供了一個比官方更友善更強大的HTTP網絡傳輸的封裝,非常的好用。

在xcode中拖一個簡單的界面,如下圖,分别調用服務端的三個方法:

定義下面三個方法與三個button的點選事件對應:

- (IBAction)fetchXML:(id)sender;

- (IBAction)fetchJson:(id)sender;

- (IBAction)createJson:(id)sender;

三個點選事件的具體實作:下面的代碼為了簡單起見,直接将xml和json輸入。比較好的做法是:在iPhone也定義一個User類,将json或者XML轉換為User實體對象。

- (IBAction)fetchXML:(id)sender

NSURL *url = [NSURL URLWithString:@"http://10.5.23.117:21924/Service1.svc/UserXML/zhuqilin"];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

[request startSynchronous];

NSError *error = [request error];

if (!error) {

NSString *response = [request responseString];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"xml格式"

message:response

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles:nil];

[alertView show];

[alertView release];

- (IBAction)fetchJson:(id)sender

NSURL *url = [NSURL URLWithString:@"http://10.5.23.117:21924/Service1.svc/User/zhuqilin"];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"JSON格式"

- (IBAction)createJson:(id)sender

NSURL *url = [NSURL URLWithString:@"http://10.5.23.117:21924/Service1.svc/User/zhuqilin/1986-09-20"];

[request setRequestMethod:@"POST"];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"建立使用者"

輸出結果:

1、擷取json格式的資料

2、擷取xml格式的資料

3、建立一個使用者實體:

總結:本文通過一個簡單的例子說明了如何在iPhone中調用wcf restful服務。你會感覺到這個方式,比去定義soap去調用要好很多,如果你有更友善的方式,請一定要告訴我哈。

本文轉自麒麟部落格園部落格,原文連結:http://www.cnblogs.com/zhuqil/archive/2011/04/26/iphone-restful-wcf.html,如需轉載請自行聯系原作者