ios SoapService请求

步骤

     1:利用SoapUI或者火狐自带插件看服务器端给出的xml 格式

     2:根据xml格式拼写soapString

     3: soapBody 格式说明

            

            <参数名>参数值

           

       4:请求soap

例子

 NSString *soapBody = [NSString stringWithFormat:
                          @"命名空间\">"
                          "参数值"
                          "参数值"
                          "%@"
                          "
",参数值];//参数
    [self SOAPData:@"服务器端地址" soapBody:soapBody success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error.debugDescription);
    }];

+ (void)SOAPData:(NSString *)url soapBody:(NSString *)soapBody success:(void (^)(id responseObject))success failure:(void(^)(NSError *error))failure {
    NSString *soapStr = [NSString stringWithFormat:
                         @"\n"
                         "命名空间" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                         "\n"
                         "%@\n"
                         "
\n"
                         "
",
                         soapBody];
    NSURL *urlString = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlString];
    
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapStr length]];
    
    [request addValue:@"命名空间/方法名" forHTTPHeaderField:@"SOAPAction"];
    [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: [soapStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
          success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
    [operation start];
}

格式基本是一样,主要soapBody内细微的地方不好调通!要有一个强大的后端把格式直接给你 ,那就完美了!!

        





你可能感兴趣的:(IOS)