//发送短信验证码URL
NSString *httpSmsAuthCodeUrl = @"http://<ip>:<port>/open/api/sms/authcode/send";
//校验短信验证码URL
NSString *httpVerifySmsAuthCodeUrl = @"http://<ip>:<port>/open/api/sms/authcode/verify";
#pragma mark 获取短信验证码
- (void)sendSmsAuthCode{
NSLog(@"go");
//短信验证码URL请求转换为NSURL
NSURL *url = [NSURL URLWithString: httpSmsAuthCodeUrl];
//创建可变http请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
//POST方式发起
[request setHTTPMethod:@"POST"];
//添加Content-type头域
[request setValue:kHttpHeaderContentTypeValue forHTTPHeaderField:kHttpHeaderContentType];
//HTTP报体数据字典
NSDictionary *dict = @{@"apiKey": @"357964ca9d6147a38746798586994a93",
@"secretKey": @"a2ef78b6d4e440239b8789856c86e9a5",
@"sourceAddr": @"1069060600860007",
@"destAddr": @"18867101821",
@"expireSeconds": @"300"};
//字典转换成NSDATA类型
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
//设置HPPT报体
[request setHTTPBody:data];
//发起同步请求,超时时间在创建HTTP请求对象时设置
NSURLResponse *response = nil;
NSError *error = nil;
//response包含HTTP请求URL,HTTP应答状态码,HTTP应答头域
//dataResp包含HTTP应答报体
NSData *dataResp = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
NSLog(@"error: %@", error);
return;
}
//dataResp转成字典,以便获取应答字段的内容
NSDictionary *dictResp = [NSJSONSerialization JSONObjectWithData:dataResp options:NSJSONReadingAllowFragments error:&error];
NSMutableString *result = [[NSMutableString alloc]initWithFormat:@"%@: %@", dictResp[@"resultCode"], dictResp[@"resultMsg"]];
//弹提示框,告知验证结果
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"获取短信验证码结果" message:result delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
return;
}
#pragma mark 校验短信验证码
- (void)verifySmsAuthCode{
NSString *smsAuthCode = self.smsText.text;
//校验短信验证码URL请求转换为NSURL
NSURL *url = [NSURL URLWithString: httpVerifySmsAuthCodeUrl];
//创建可变http请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
//POST方式发起
[request setHTTPMethod:@"POST"];
//添加Content-type头域
[request setValue:kHttpHeaderContentTypeValue forHTTPHeaderField:kHttpHeaderContentType];
//HTTP报体数据字典
NSDictionary *dict = @{@"apiKey": @"357964ca9d6147a38746798586994a93",
@"destAddr": @"18867101821",
@"authCode": smsAuthCode};
//字典转换成NSDATA类型
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
//设置HPPT报体
[request setHTTPBody:data];
//发起同步请求,超时时间在创建HTTP请求对象时设置
NSURLResponse *response = nil;
NSError *error = nil;
//response包含HTTP请求URL,HTTP应答状态码,HTTP应答头域
//dataResp包含HTTP应答报体
NSData *dataResp = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
NSLog(@"error: %@", error);
return;
}
//dataResp转成字典,以便获取应答字段的内容
NSDictionary *dictResp = [NSJSONSerialization JSONObjectWithData:dataResp options:NSJSONReadingAllowFragments error:&error];
NSMutableString *result = [[NSMutableString alloc]initWithFormat:@"%@: %@", dictResp[@"resultCode"], dictResp[@"resultMsg"]];
//弹提示框,告知验证结果
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"验证结果" message:result delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
return;
}
中移贯众开放平台短信验证码开发说明:
http://openservice.com.cn/open_devportal/authcode.action
运行效果如下图所示。
点击图标获取验证码。
输入验证码后,点击验证,会得到验证结果。