IOS -- 基于WebService WSDL 的请求

公司的WebService真的把我搞惨了。还好ok了。

感谢 http://wuchaorang.2008.blog.163.com/blog/static/48891852201391695530894/ 对我的帮助。


先看清楚大前提

1. WebService 用的是soap 1.1



切入正题:

1.写出soap字符串进行请求,十分重要 我调用的是soap1.1。 soap1.2与soap1.1要写的soap体不一样。

详细区别可见 http://brucexx.iteye.com/blog/1490422

<span style="white-space:pre">			</span>"<?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/\" "
                         "xmlns:ns1=\"(此处为webservice的命名空间)">\"\n"
                         "<soap:Body>"
                        
                         "<ns1:findProductDetail>" <span style="font-family: Arial, Helvetica, sans-serif;">(sn1:为上面的命名空间 此处填写的是你的方面名)</span>
                         "<productId>20000</productId>" <span style="font-family: Arial, Helvetica, sans-serif;">(此处填写的是你的参数名以及参数值)</span>
                         "</ns1:findProductDetail>"
                         
                         "</soap:Body>"
                         "</soap:Envelope>"


2.Soap体搞定后 就可以请求了。请求后再用NSXMLParser或者GDataXMLNode来解析XML  详细IOS代码见下(只有请求代码)

    NSString *number = @"20000";
    NSString *soapMsg = [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/\" "
                         "xmlns:ns1=\"http://你要用到的命名空间\">\"\n"
                         "<soap:Body>"
                        
                         "<ns1:findProductDetail>"
                         "<productId>%@</productId>"
                         "</ns1:findProductDetail>"
                         
                         "</soap:Body>"
                         "</soap:Envelope>", number];
    NSLog(@"%@",soapMsg);
    
    NSURL *url = [NSURL URLWithString:@"你要请求的网址"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMsg length]];
    // 添加请求的详细信息,与请求报文前半部分的各字段对应
    // soap1.1 addValue:@"text/xml      soap1.2用addValue:@"application/soap+xml“
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    // 设置请求行方法为POST,与请求报文第一行对应
    [req setHTTPMethod:@"POST"];
    // 将SOAP消息加到请求中
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
    // 创建连接
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) {
        NSLog(@"NSURLConnection is ok \n");
        webData = [NSMutableData data];
    }

最后你在NSURLConnectionDelegate 的方法里取值webData的值就ok了



希望能帮助到需要的人。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值