使用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)

  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.         
  25.         [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
  26.         [theRequest setHTTPMethod:@"POST"];
  27.         [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
  28.         
  29.         //请求
  30.         NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  31.         
  32.         //如果连接已经建好,则初始化data
  33.         if( theConnection )
  34.         {
  35.                 webData = [[NSMutableData data] retain];
  36.         }
  37.         else
  38.         {
  39.                 NSLog(@"theConnection is NULL");
  40.         }
  41.         
  42.         
  43. }
2 通过ASI调用webService 其实和传统的方式基本一样,只是做了相关的封装而已,具体的请参考下面的文章
http://www.cocoachina.com/bbs/read.php?tid=98388&keyword=http

原创粉丝点击