求指教: iOS 调用 soap webservice,一直调用不成功,求大神指导,代码如下~~

2015-11-18 12:40:22 +08:00
 lfflyers
NSString *sSOAPMessage = [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"
"<soapenv:Body>\n"
"<Security>%@</Security>\n"
"<GetMeasuredWaterlevelStationCatalog xmlns=\"http://www.cjienc.com/CjiencForInstitute/\">\n"
"</GetMeasuredWaterlevelStationCatalog>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",@"950C65C373EA4A1497812FF32D89B10A81863D"
];;

NSURL *sRequestURL = [NSURL URLWithString:@"http://www.cjienc.com/axis2/services/CjiencForInstitute?wsdl"];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:sRequestURL];
NSString *sMessageLength = [NSString stringWithFormat:@"%d", [sSOAPMessage length]];

[myRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[myRequest addValue: @"http://www.cjienc.com/CjiencForInstitute/GetMeasuredWaterlevelStationCatalog" forHTTPHeaderField:@"SOAPAction"];
[myRequest addValue: sMessageLength forHTTPHeaderField:@"Content-Length"];
[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody: [sSOAPMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];

if( theConnection ) {
self.webResponseData = [NSMutableData data];
}else {
NSLog(@"Some error occurred in Connection");

}


GetMeasuredWaterlevelStationCatalog 是请求方法。
打印 Log 如下:
2015-11-18 12:38:13.716 WebServiceDemo[1102:106763] <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "soapenv"&#xd;
at [row,col {unknown-source}]: [3,14]</faultstring><detail><Exception>org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "soapenv"&#xd;
at [row,col {unknown-source}]: [3,14]&#xd;
4437 次点击
所在节点    iDev
15 条回复
xi_lin
2015-11-18 12:42:08 +08:00
选 markdown 模式排版一下代码吧。。
xi_lin
2015-11-18 12:43:18 +08:00
粗看感觉是你的请求有问题,返回都说 org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "soapenv"了
lfflyers
2015-11-18 12:45:46 +08:00
@xi_lin 我没有用 soapenv ,我用的是 soap
xi_lin
2015-11-18 12:56:45 +08:00
@lfflyers 你有一行"<soapenv:Body>\n" 啊
lfflyers
2015-11-18 13:04:29 +08:00
@xi_lin 是的,谢谢,那个是我写错了,把<soapenv:Body>\n 改正之后又出现 Web service 请求没有安全验证的信息 ,后台叫在 body 里加一个 Security 的字段我也加了,可就是请求不成功
xi_lin
2015-11-18 13:12:12 +08:00
@lfflyers 这种协议的交互你和后台对呗,看验证的需要。
xi_lin
2015-11-18 13:17:42 +08:00
@lfflyers axis2 可以让后台生成一个 java client 你照着发请求,也可以试试上传 wsdl 到这 http://sudzc.com/生成一个 oc client 。后者我没用过不知道效果怎么样。
hohoho
2015-11-18 13:29:37 +08:00
让提供 webservice 的同事给你一份接口的 wsdl ,然后你根据这份说明构造请求和处理返回的数据,尽可能的用复制代替手写,这种错误最难找,别问我为什么。

另外网上相关的帮助有很多,你碰到的基本都能找到解决方案
lfflyers
2015-11-18 14:32:01 +08:00
@xi_lin 谢谢,问题解决了 要加个头部验证在 body 前加上<soap:Header><Security>xxxxxxx</Security></soap:Header>
lfflyers
2015-11-18 14:32:11 +08:00
@hohoho 谢谢,问题解决了 要加个头部验证在 body 前加上<soap:Header><Security>xxxxxxx</Security></soap:Header>
lfflyers
2015-11-18 17:07:36 +08:00
@xi_lin 又有个问题了~~当我加了参数,代码如下:
"<GetMeasuredWaterlevelInfo xmlns=\"http://www.cjienc.com/CjiencForInstitute/\">\n"
"<stationCode>%@</stationCode>"
"<startTime>%@</startTime>"
"<endTime>%@</endTime>"
"</GetMeasuredWaterlevelInfo>\n"

报以下错误:
ADBException: Unexpected subelement {http://www.cjienc.com/CjiencForInstitute/}stationCode</faultstring><detail><Exception>org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement {http://www.cjienc.com/CjiencForInstitute/}stationCode&#xd;

真心搞不懂,参数跟 Java 的一样可就是不行
xi_lin
2015-11-18 17:28:06 +08:00
@lfflyers 你看错误提示啊少年, stationCode 你的接口不认我也没有办法
lfflyers
2015-11-18 17:42:41 +08:00
@xi_lin 找了 N 多的资料,感觉是服务器那边的问题,蛋就蛋疼在服务器不在我们这里,根本没办法交流
beginor
2015-11-18 22:37:05 +08:00
手写 SOAP 简直就是浪费生命, 服务端想办法转 REST 吧
chenxiaohong
2015-11-18 23:09:54 +08:00
这是手写的?没有服务发布发址吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/237018

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX