iOS另一种获取soap的方法

首先新建类WebServices

WebServices.h

#import <Foundation/Foundation.h>

@interface WebServiceHelper : NSObject

@property (nonatomic,strong) NSString *MethodName;

@property (nonatomic,strong) NSString *SoapUrlAddress;

@property (nonatomic,strong) NSMutableDictionary *MethodKeyAndValues;

@property (nonatomic,strong) NSString *XmlNameSpace;

-(NSString *)Post;

@end

WebServices.m

#import "WebServiceHelper.h"

@implementation WebServiceHelper

- (instancetype)init
{
    self = [super init];
    if (self) {
        [email protected]"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

        [email protected]"http://WebXml.com.cn/";
    }
    return self;
}

-(NSString *)Post
{
    NSString *xml=[[NSString alloc]init];

    NSMutableString *soapMsg=[[NSMutableString alloc]init];
    [soapMsg appendFormat:@"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "];
    [soapMsg appendFormat:@"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>"];
    [soapMsg appendFormat:@"<%@ xmlns=\"%@\">",self.MethodName,self.XmlNameSpace];

    NSArray *keys=[self.MethodKeyAndValues allKeys];
    int length=[keys count];

    for (int i=0; i<length; i++) {
        id key=[keys objectAtIndex:i];
        id value=[self.MethodKeyAndValues objectForKey:key];
        [soapMsg appendFormat:@"<%@>%@</%@>",key,value,key];
    }

    [soapMsg appendFormat:@"</%@></soap:Body></soap:Envelope>",self.MethodName];

    NSLog(@"%@",soapMsg);

    NSURL *url=[NSURL URLWithString:self.SoapUrlAddress];

    NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];

    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    NSString *soapAction=[[NSString alloc]initWithFormat:@"%@%@",self.XmlNameSpace,self.MethodName];

    [req addValue:soapAction forHTTPHeaderField:@"SOAPAction"];

    NSLog(@"%@",soapAction);

    [req setHTTPMethod:@"POST"];

    [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLResponse *response=nil;
    NSError *error=nil;
    NSData *result=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
    if (error) {
        NSLog(@"Connection Error: %@", [error description], nil);
    }

    xml=[[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];
    return xml;
}

@end

调用方法

WebServiceHelper *helper=[[WebServiceHelper alloc]init];

    [email protected]"getMobileCodeInfo";

    NSMutableDictionary *keyAndValues=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"123456789",@"mobileCode",@"",@"userid",nil];

    helper.MethodKeyAndValues=keyAndValues;

    NSString *value=[helper Post];

    NSLog(@"%@",value);

与前一个比的好处就是,不用委托了。因为是初学,大家多提意见。

时间: 2024-08-02 12:57:12

iOS另一种获取soap的方法的相关文章

6.Request的三种获取数据的方法

其实一共有4种,不过form这种专门获取以post方式提交数据的方式目前还没学.所以就不太演示了 另外需要注意的是,Request.params 这种获取方式能不用就不要用了,有时候会出问题 那么代码如下: 第一个页面是index.aspx的代码: 第一个页面的index.aspx.cs的代码: 这里要说一下,上面这行语句,我在问号后面多打了一个空格,结果运行的时候就报错了!!!说没找到页面! 第二个页面的test.aspx.cs的代码: 原文地址:https://www.cnblogs.com

记录两种获取配置文件的方法

1.classLoader得到配置文件的数据 package com.spring.jdbc.jdbcTemplate; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * @author 作者 : 程呈 * @version 创建时间:2017年7月8日 下午6:45:46 类说明 */ public class TestGetProValue { public s

UITableViewCell的两种获取indexPath的方法

1. 通过叫Point获取 CGPoint correctedPoint = [textField convertPoint:textField.bounds.origin toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint]; NSLog(@"Button tapped in row %ld", (long)indexPath.ro

Java中获取路径的方法_自我分析

就目前的我来说最常用的两种获取路径的方法是  class.getRecource(filename) 和 class.getclassloader.getRecource(filename) 这两者的区别其实很简单就是路径的时候有点不同,这里主要讲两个参数,其他的路径获取,其他的话在根据相对路径逐一查找就行了 class.getRecource(filename): 参数"/" 表示获取根目录; (即我们常用到的bin目录[字节码文件存放的目录] " "  表示获取

iOS中有3种常见的多线程编程方法

1.NSThread 这种方法需要管理线程的生命周期.同步.加锁问题,会导致一定的性能开销 2.NSOperation和NSOperationQueue 是基于OC实现的.NSOperation以面向对象的方式封装了需要执行的操作,然后可以将这个操作放到一个NSOperationQueue中去异步执行.不必关心线程管理.同步等问题. 3.Grand Centeral Dispatch 简称GCD,iOS4才开始支持,是纯C语言的API.自iPad2开始,苹果设备开始有了双核CPU,为了充分利用这

iOS - UITableView中有两种重用Cell的方法

UITableView中有两种重用Cell的方法: iOS代码 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 在iOS 6中dequeueReusableCellWith

IOS设置frame的时候经常要先取出来-&gt; 设置-&gt; 最后再赋值回去,非常麻烦,今天给大家推荐一种非常快捷的方法

大家可以去我的Githup下载   https://github.com/simplyou/YJ-UIIView-/tree/master 在设置尺寸的时候亲们有没有感觉很蛋疼啊,这里提供了一套分类,直接放进工程里,在PCH中包含头文件就能解决你蛋疼的问题; /***********************  .h文件   ******************************** //  UIView+YJ.h //  Created by 闪电 on 14-6-8. //  Copyr

[转]SQL三种获取自增长的ID方法

SQL SERVER中的三种获得自增长ID的方法  这个功能比较常用,所以记下来以防自己忘掉. SCOPE_IDENTITY 返回插入到同一作用域中的 IDENTITY 列内的最后一个 IDENTITY 值.一个作用域就是一个模块--存储过程.触发器.函数或批处理.因此,如果两个语句处于同一个存储过程.函数或批处理中,则它们位于相同的作用域中. IDENT_CURRENT  返回为任何会话和任何作用域中的指定表最后生成的标识值.这个函数需要一个以表名为值的变量,也就是说虽然不受会话和作用域的限制

对几种获取字符串长度的方法进行性能比较

测试环境: 操作系统:CentOS release 6.8 (Final) 操作环境:vi编辑器 任务:对获取字符串长度的几种统计方法的性能比较. 测试数据如下: 1.变量自带的获取长度的方法 [[email protected] scripts]# time for n in {1..10000};do char=`seq -s "skyboy" 100`;echo ${#char} &>/dev/null;done real    0m19.712s user