求指教: iOS 调用 soap webservice,一直调用不成功,求大神指导,代码如下~~ - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
lfflyers
V2EX    iDev

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

  •  1
     
  •   lfflyers 2015-11-18 12:40:22 +08:00 5002 次点击
    这是一个创建于 3625 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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=" 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;
    15 条回复    2015-11-18 23:09:54 +08:00
    xi_lin
        1
    xi_lin  
       2015-11-18 12:42:08 +08:00
    选 markdown 模式排版一下代码吧。。
    xi_lin
        2
    xi_lin  
       2015-11-18 12:43:18 +08:00
    粗看感觉是你的请求有问题,返回都说 org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "soapenv"了
    lfflyers
        3
    lfflyers  
    OP
       2015-11-18 12:45:46 +08:00
    @xi_lin 我没有用 soapenv ,我用的是 soap
    xi_lin
        4
    xi_lin  
       2015-11-18 12:56:45 +08:00
    @lfflyers 你有一行"<soapenv:Body>\n" 啊
    lfflyers
        5
    lfflyers  
    OP
       2015-11-18 13:04:29 +08:00
    @xi_lin 是的,谢谢,那个是我写错了,把<soapenv:Body>\n 改正之后又出现 Web service 请求没有安全验证的信息 ,后台叫在 body 里加一个 Security 的字段我也加了,可就是请求不成功
    xi_lin
        6
    xi_lin  
       2015-11-18 13:12:12 +08:00
    @lfflyers 这种协议的交互你和后台对呗,看验证的需要。
    xi_lin
        7
    xi_lin  
       2015-11-18 13:17:42 +08:00
    @lfflyers axis2 可以让后台生成一个 java client 你照着发请求,也可以试试上传 wsdl 到这 http://sudzc.com/生成一个 oc client 。后者我没用过不知道效果怎么样。
    hohoho
        8
    hohoho  
       2015-11-18 13:29:37 +08:00 via iPhone
    让提供 webservice 的同事给你一份接口的 wsdl ,然后你根据这份说明构造请求和处理返回的数据,尽可能的用复制代替手写,这种错误最难找,别问我为什么。

    另外网上相关的帮助有很多,你碰到的基本都能找到解决方案
    lfflyers
        9
    lfflyers  
    OP
       2015-11-18 14:32:01 +08:00
    @xi_lin 谢谢,问题解决了 要加个头部验证在 body 前加上<soap:Header><Security>xxxxxxx</Security></soap:Header>
    lfflyers
        10
    lfflyers  
    OP
       2015-11-18 14:32:11 +08:00
    @hohoho 谢谢,问题解决了 要加个头部验证在 body 前加上<soap:Header><Security>xxxxxxx</Security></soap:Header>
    lfflyers
        11
    lfflyers  
    OP
       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
        12
    xi_lin  
       2015-11-18 17:28:06 +08:00
    @lfflyers 你看错误提示啊少年, stationCode 你的接口不认我也没有办法
    lfflyers
        13
    lfflyers  
    OP
       2015-11-18 17:42:41 +08:00
    @xi_lin 找了 N 多的资料,感觉是服务器那边的问题,蛋就蛋疼在服务器不在我们这里,根本没办法交流
    beginor
        14
    beginor  
       2015-11-18 22:37:05 +08:00
    手写 SOAP 简直就是浪费生命, 服务端想办法转 REST 吧
    chenxiaohong
        15
    chenxiaohong  
       2015-11-18 23:09:54 +08:00
    这是手写的?没有服务发布发址吗
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3459 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 00:41 PVG 08:41 LAX 17:41 JFK 20:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86