使用ASIHttpRequest调用WebService
来源:互联网 发布:如何开淘宝 编辑:程序博客网 时间:2024/05/18 09:30
在项目中用到了好多的调用WebSerViece的请求的地方,一直用系统的 NSMutableURLRequest 和NSURLConnection结合实现的,这样做有一定的好处,原生态,不会过时。
但是有时你获取需要实现一定的效果,用系统的虽然也能实现,但比较麻烦,除非自己封装,要不每次都的重写,ASI是比较好的网络请求开源框架,用的人比较多,遗憾的是已经停止更新,(据说在ios7 下有些问题,暂时还没有研究,等ios7正式发布以后在具体的试试),暂时还是能用的,这里就说说怎么用ASIHttpRequest 调用WebService
1、传统的调用方式(具体的你可以参考http://www.cocoachina.com/bbs/read.php?tid=16561&keyword=soap)
- - (void)getOffesetUTCTimeSOAP
- {
- recordResults = NO;
- //封装soap请求消息
- NSString *soapMessage = [NSString stringWithFormat:
- @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
- "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
- "<soap:Body>\n"
- "<getOffesetUTCTime xmlns=\"http://www.Nanonull.com/TimeService/\">\n"
- "<hoursOffset>%@</hoursOffset>\n"
- "</getOffesetUTCTime>\n"
- "</soap:Body>\n"
- "</soap:Envelope>\n",nameInput.text
- ];
- NSLog(soapMessage);
- //请求发送到的路径
- NSURL *url = [NSURL URLWithString:@"http://www.nanonull.com/TimeService/TimeService.asmx"];
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
- NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
- //以下对请求信息添加属性前四句是必有的,第五句是soap信息。
- [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
- [theRequest addValue: @"http://www.Nanonull.com/TimeService/getOffesetUTCTime" forHTTPHeaderField:@"SOAPAction"];
- [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
- [theRequest setHTTPMethod:@"POST"];
- [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
- //请求
- NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
- //如果连接已经建好,则初始化data
- if( theConnection )
- {
- webData = [[NSMutableData data] retain];
- }
- else
- {
- NSLog(@"theConnection is NULL");
- }
- }
2 通过ASI调用webService 其实和传统的方式基本一样,只是做了相关的封装而已,具体的请参考下面的文章
http://www.cocoachina.com/bbs/read.php?tid=98388&keyword=http
- 使用ASIHttpRequest调用WebService
- 使用ASIHttpRequest调用WebService
- iOS 使用ASIHttpRequest调用WebService
- iOS学习之-使用ASIHttpRequest调用WebService
- iOS学习之2-使用ASIHttpRequest调用WebService
- iOS学习之2-使用ASIHttpRequest调用WebService
- iOS学习之2-使用ASIHttpRequest调用WebService
- 基于SOAP协议并使用ASIHTTPRequest调用Webservice完成数据请求。
- iOS 使用ASIHTTPRequest 发送Webservice请求
- 使用JS调用WebService
- 使用Js调用WebService
- 使用Curl调用WebService
- 使用KSOAP2调用WebService
- 使用ajax调用webservice
- 使用URLConnection调用webservice
- 使用ajax调用webservice
- 使用URLConnection调用webservice
- 使用javascript调用webservice
- 几个关键字auto register extern简介
- 黑马程序员_02_运算符_进制转换
- 苹果应用商店审核指南2013版
- ASSIC码对照表
- 机顶盒市场:尚未长大 乱象已生
- 使用ASIHttpRequest调用WebService
- ProgressDialog的基本用法(一)
- Dropbox正在成为互联网基础设施,网盘们呢?
- 基础笔记
- 实现一个栈,使push,pop,min操作只需要o(1)时间
- VS2010在命令行中使用
- Spring 开发集合
- 网络协议栈深入分析(四)--套接字内核初始化和创建过程
- UNIX Philosophy