您的位置:首页 > 移动开发 > IOS开发

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

2015-12-11 09:02 761 查看
原文转自: http://blog.csdn.net/huang2009303513/article/details/41054933
问:
This is my current call to (asmx) SOAP web service:

[objc] view
plaincopy





NSString *soapMessage =

[NSString stringWithFormat:

@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"

"<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/\">"

"<soap:Body>"

"<Save xmlns=\"http://www.myapp.com/\">"

"<par1>%i</par1>"

"<par2>%@</par2>"

"<par3>%@</par3>"

"</Save>"

"</soap:Body>"

"</soap:Envelope>", par1, par2, par3

];

NSURL *url = [NSURL URLWithString:@"http://....asmx"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

[request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];

[request setHTTPMethod:@"POST"];

[request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

if([self.delegate respondsToSelector:@selector(myAppHTTPClientDelegate:didUpdateWithWeather:)]){

[self.delegate myAppHTTPClientDelegate:self didUpdateWithWeather:responseObject];

}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

if([self.delegate respondsToSelector:@selector(myAppHTTPClientDelegate:self:didFailWithError:)]){

[self.delegate myAppHTTPClientDelegate:self didFailWithError:error];

}

}];

[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://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.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://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9" 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
plaincopy





NSDictionary *request = @{@"email": self.email.text, @"password": self.password.text};

[manager POST:login parameters:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"DONE!");

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Failed to log in: %@", operation.responseString);

}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: