使用ios调用web Service成功案例
来源:互联网 发布:网络推广公司铭心营销 编辑:程序博客网 时间:2024/05/20 11:49
实现两个int类型相加返回和,在使用的时候导入需要用到的类库,具体使用详见:
http://blog.csdn.net/iosweb/article/details/49593997
代码截图:
代码如下:
- (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格式的。
0 0
- 使用ios调用web Service成功案例
- ios调用web service
- php5调用web service (笔者测试成功)
- 使用Web Service 调用Web Service
- 使用java调用Web-Service
- 使用Axis2调用Web Service
- 使用PHP调用Web service
- 使用Python调用Web Service
- 使用Java调用Web Service
- 使用Python调用Web Service
- IOS下使用WEB SERVICE
- iOS 开发之 Web Service 调用
- 使用XFire调用Web服务 测式 案例!
- 使用Web Service 调用两地的数据
- abap 调用外部web service 使用小结
- Biztalk使用WCF Adapter调用Web Service
- 使用JQuery + JSON 调用Web Service
- abap 调用外部web service 使用小结
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”
- zookeeper能干什么
- tolua总结(一)
- 鸟哥的Linux私房菜(基础篇)-第二章、 Linux 如何学习(二.2. 鸟哥的Linux苦难经验全都录)
- 工作中遇到的问题一
- 使用ios调用web Service成功案例
- hadoop开发环境搭建
- socket错误使用recvfrom 返回-1的问题
- python打开文件对话框
- 买车砍价
- 鸟哥的Linux私房菜(基础篇)-第二章、 Linux 如何学习(二.3. 有心朝Linux作业系统学习者的学习态度)
- 导航算路
- Java之美[从菜鸟到高手演变]之设计模式二
- 《疯狂Android讲义》读书笔记1.Android应用与开发环境