摘自自己csdn博文,是直接copy過來的,請見諒,希望多多交流:
最近開始接觸ios項目的開發。之前使用本地數據庫做的是offline的project。現在開始嘗試去從其他平台上拿數據。ios project是基於mac系統上的。但需要去調用windows系統上的服務。微軟有一個不錯的框架wcf.話不多說,直接看代碼吧! 首先是wcf服務。 數據交換實體(字數限制,代碼略) 服務實現(字數限制,代碼略)
在web.config中将绑定方式改成webHttpBinding 服務器上發佈服務(iis) 接下來ios端 使用ASIHTTPRequest,是直接在CFNetwork上做的开源项目,提供了一个很方便更强大的HTTP网络传输的封装。 定义下面三个方法對應三个button的点击事件: - - (IBAction)fetXML:(id)sender;
- - (IBAction)fetJson:(id)sender;
- - (IBAction)createJson:(id)sender;
- 具體實現
- - (IBAction)fetXML:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://192.168.112.220:2194/Service1.svc/CustomerXML/Koson"];
- 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)fetJson:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://192.168.112.220:2194/Service1.svc/Customer/Koson"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request startSynchronous];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"json格式"
- message:response
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- [alertView release];
- }
- }
- - (IBAction)createJson:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://192.168.112.220:2194/Service1.svc/Customer/Koson/2006-09-20"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setRequestMethod:@"POST"];
- [request startSynchronous];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"创建k客戶"
- message:response
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- [alertView release];
- }
- }
复制代码 |