使用ios调用web Service成功案例
2015-11-03 12:22
477 查看
实现两个int类型相加返回和,在使用的时候导入需要用到的类库,具体使用详见:
/article/9539477.html
代码截图:
代码如下:
需要注意的2个参数:
//注意这里的参数,是名字空间加上接口方法名
下面还要我讲吗?:)接收服务器反馈数据的方法已经赤裸裸的写在代码的最后了,这个方法:
看名字也知道它的作用。在里面得到反馈信息就OK了。返回的数据是XML格式的。
/article/9539477.html
代码截图:
代码如下:
- (void)viewDidLoad { [super viewDidLoad]; int i = 5; int j = 12; NSString *soapMessage = [NSString stringWithFormat: @"<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'>\n" "<soap12:Body>\n" "<add xmlns='http://tempuri.org/'>\n" "<a>%i</a>" "<b>%i</b>" "</add>\n"//add是方法名 "</soap12:Body>\n" "</soap12:Envelope>\n",i,j ]; // url端口html NSURL *url = [NSURL URLWithString: @"http://m.93966.net:1133/BmIndexPageWeb/wsTest.asmx"]; req = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]]; [req addValue:@"http://tempuri.org/add" forHTTPHeaderField:@"SOAPAction"]; [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; [req setHTTPMethod:@"POST"]; [req setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; if(conn) { webData = [NSMutableData data]; } } - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength:0]; } - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; // data转换成字符串类型 // NSString* aStr= [[NSString alloc] initWithData:webData encoding:NSASCIIStringEncoding]; // NSLog(@"%@",aStr); } // 当请求失败时的相关操作; - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",error); } //回调 - (void) connectionDidFinishLoading:(NSURLConnection *) connection { // NSLog(@"Done. Received Bytes: %lu", (unsigned long)[webData length]); NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; NSLog(@"返回的结果: %@", theXML); }
需要注意的2个参数:
NSURL *url = [NSURL URLWithString: @"http://m.93966.net:1133/BmIndexPageWeb/wsTest.asmx"]; //Web Service 的RUL地址 [req addValue:@"http://tempuri.org/add" forHTTPHeaderField:@"SOAPAction"];
//注意这里的参数,是名字空间加上接口方法名
下面还要我讲吗?:)接收服务器反馈数据的方法已经赤裸裸的写在代码的最后了,这个方法:
- (void) connectionDidFinishLoading:(NSURLConnection *) connection
看名字也知道它的作用。在里面得到反馈信息就OK了。返回的数据是XML格式的。
相关文章推荐
- FFMPeg-ios编译
- iOS9 适配
- [IOS]添加预编译文件(.pch)
- IOS开发: 使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS 优秀博客
- iOS 获取验证码倒计时
- IOS学习之AFNetworking 使用总结 (用法+JSON解析)
- iOS开发中 自定义TabBar
- GitHub上值得关注的iOS开源项目
- ios系统地图无法定位问题
- iOS 容易引“起循环引用”的三种场景
- iOS开发笔记--导致EXC_BAD_ACCESS问题的原因
- IOS CoreData
- 2015-11-3 IOS 开发者账号申请
- ios 字号问题
- 【iOS】汉字数组,按首字母排序
- iOS视图生命周期
- xcode7.1不能运行ios7模拟器问题
- iOS: ARC和非ARC下使用Block属性的问题
- iOS实现多个异步线程同步的操作