iOS开发-进阶:SOAP web service用AFNetWorking实现请求

原文转自: http://blog.csdn.net/huang2009303513/article/details/41054933


问:

This is my current call to (asmx) SOAP web service:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. NSString *soapMessage =  
  2.     [NSString stringWithFormat:  
  3.      @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"  
  4.      "<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/\">"  
  5.      "<soap:Body>"  
  6.      "<Save xmlns=\"http://www.myapp.com/\">"  
  7.      "<par1>%i</par1>"  
  8.      "<par2>%@</par2>"  
  9.      "<par3>%@</par3>"  
  10.      "</Save>"  
  11.      "</soap:Body>"  
  12.      "</soap:Envelope>", par1, par2, par3  
  13.      ];  
  14. NSURL *url = [NSURL URLWithString:@"http://....asmx"];  
  15.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  16.   
  17.     NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];  
  18.   
  19.     [request addValue@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
  20.     [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];  
  21.   
  22.     [request setHTTPMethod:@"POST"];  
  23.     [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  
  24.   
  25. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
  26. operation.responseSerializer = [AFXMLParserResponseSerializer serializer];  
  27. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  28.         if([self.delegate respondsToSelector:@selector(myAppHTTPClientDelegate:didUpdateWithWeather:)]){  
  29.             [self.delegate myAppHTTPClientDelegate:self didUpdateWithWeather:responseObject];  
  30.         }           
  31.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {              
  32.         if([self.delegate respondsToSelector:@selector(myAppHTTPClientDelegate:self:didFailWithError:)]){  
  33.             [self.delegate myAppHTTPClientDelegate:self didFailWithError:error];  
  34.         }  
  35.     }];  
  36.   
  37.     [operation start];  

But I need to change this to use  AFHTTPSessionManager .
I suppose that I need to use this:

<p>UPDATE</p><pre class="lang-c prettyprint prettyprinted" name="code" style="white-space: pre-wrap; word-wrap: break-word;"><div class="dp-highlighter bg_objc" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: rgb(231, 229, 220); width: 700.90625px; overflow: auto; padding-top: 1px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108);"><strong>[objc]</strong> <a target=_blank href="http://blog.csdn.net/huang2009303513/article/details/41054933#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">view plain</a><a target=_blank href="http://blog.csdn.net/huang2009303513/article/details/41054933#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">copy</a><a target=_blank href="https://code.csdn.net/snippets/517123" target="_blank" title="在CODE上查看代码片" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/517123/fork" target="_blank" title="派生到我的代码片" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 376px; top: 1467px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=18&height=18" wmode="transparent" /></div></div></div><ol start="1" class="dp-objc" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">NSDictionary</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *s_request = @{</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"par1"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">: [NSString</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> stringWithFormat</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"%i"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> par1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">], </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"par2"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">: par</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"par3"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">: par</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"par4"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">: [NSString</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> stringWithFormat</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"%i"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> par4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">], </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"par5"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">: par</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">};  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    AFHTTPSessionManager* s_manager = [[AFHTTPSessionManager<span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> alloc</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> init</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">];  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    [s_manager<span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> POST</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"http://192.168.10.26/mywebservice/myservice.asmx?op=MethodName"</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> parameters</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:s_request</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> success</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:^(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">NSURLSessionDataTask</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *task, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">id</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> responseObject) {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        NSLog(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"DONE!"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }<span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> failure</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:^(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">NSURLSessionDataTask</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *task, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">NSError</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *error) {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        NSLog(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"FAILED %@, %@"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, [error</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> localizedDescription</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">], [error</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> localizedFailureReason</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }];  </span></li></ol></div>

This code always fail. Getting error 500. Do I just need to add method URL or I need to add complete soap message somewhere. What I miss here?


 


答:

You need to pass parameter in Dictionary like in the following example:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. NSDictionary *request = @{@"email"self.email.text@"password"self.password.text};  
  2. [manager POST:login parameters:request success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  3.   NSLog(@"DONE!");  
  4. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  5.     NSLog(@"Failed to log in: %@", operation.responseString);  
  6. }];  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值