现在的位置: 首页 > 综合 > 正文

IOS — 基于WebService WSDL 的请求

2018年08月04日 ⁄ 综合 ⁄ 共 2128字 ⁄ 字号 评论关闭

公司的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了

希望能帮助到需要的人。

抱歉!评论已关闭.