微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用ASIHttpRequest调用WebService

在项目中用到了好多的调用WebSerViece的请求的地方,一直用系统的 NSMutableuRLRequest 和NSURLConnection结合实现的,这样做有一定的好处,原生态,不会过时。

但是有时你获取需要实现一定的效果,用系统的虽然也能实现,但比较麻烦,除非自己封装,要不每次都的重写,ASI是比较好的网络请求开源框架,用的人比较多,遗憾的是已经停止更新,(据说在ios7 下有些问题,暂时还没有研究,等ios7正式发布以后在具体的试试),暂时还是能用的,这里就说说怎么用ASIHttpRequest 调用WebService


1、传统的调用方式(具体的你可以参考http://www.cocoachina.com/bbs/read.php?tid=16561&keyword=soap)

  1. - (void)getoffesetUTCTimeSOAP
  2. {
  3.         recordResults = NO;
  4.         //封装soap请求消息
  5.         Nsstring *soapMessage = [Nsstring stringWithFormat:
  6.                                                          @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  7.                                                          "<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"
  8.                                                          "<soap:Body>\n"
  9.                                                          "<getoffesetUTCTime xmlns=\"http://www.Nanonull.com/TimeService/\">\n"
  10.                                                          "<hoursOffset>%@</hoursOffset>\n"
  11.                                                          "</getoffesetUTCTime>\n"
  12.                                                          "</soap:Body>\n"
  13.                                                          "</soap:Envelope>\n",nameInput.text
  14.                                                          ];
  15.         NSLog(soapMessage);
  16.         //请求发送到的路径
  17.         NSURL *url = [NSURL URLWithString:@"http://www.nanonull.com/TimeService/TimeService.asmx"];
  18.         NSMutableuRLRequest *theRequest = [NSMutableuRLRequest requestWithURL:url];
  19.         Nsstring *msgLength = [Nsstring stringWithFormat:@"%d",[soapMessage length]];
  20.         
  21.         //以下对请求信息添加属性前四句是必有的,第五句是soap信息。
  22.         [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  23.         [theRequest addValue: @"http://www.Nanonull.com/TimeService/getoffesetUTCTime" forHTTPHeaderField:@"SOAPAction"];
  24.         [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
  25.         [theRequest setHTTPMethod:@"POST"];
  26.         [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
  27.         //请求
  28.         NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  29.         //如果连接已经建好,则初始化data
  30.         if( theConnection )
  31.         {
  32.                 webData = [[NSMutableData data] retain];
  33.         }
  34.         else
  35.                 NSLog(@"theConnection is NULL");
  36. }
2 通过ASI调用webService 其实和传统的方式基本一样,只是做了相关的封装而已,具体的请参考下面的文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐