文章标题

原创 2015年11月18日 21:31:47
//注册请求
- (void)postRequest {
    //POST请求 请求参数放在请求内部(httpBody)     //设置请求
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
    request.timeoutInterval = 10;
    request.HTTPMethod = @"POST";
    request.URL = [NSURL URLWithString:@"https://api.cn.ronghub.com/user/getToken.json"];
    NSString * appkey = @"3argexb6rv2me";
    NSString * nonce = [NSString stringWithFormat:@"%d",arc4random()];
    NSString * timestamp = [[NSString alloc] initWithFormat:@"%ld",(NSInteger)[NSDate timeIntervalSinceReferenceDate]];   //配置http header
    [request setValue:appkey forHTTPHeaderField:@"App-Key"];
    [request setValue:nonce forHTTPHeaderField:@"Nonce"];
    [request setValue:timestamp forHTTPHeaderField:@"Timestamp"];
    [request setValue:@"SnJ02OMhhD2Yv" forHTTPHeaderField:@"appSecret"];     //生成hashcode 用以验证签名
    [request setValue:[self sha1:[NSString stringWithFormat:@"%@%@%@",appkey,nonce,timestamp]] forHTTPHeaderField:@"Signature"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
    [paramDic setObject:@"graveyards1215@hotmail.com" forKey:@"userId"];
    [paramDic setObject:@"graveyards" forKey:@"name"];
    [paramDic setObject:@"1.png" forKey:@"portraitUri"];
    request.HTTPBody = [self httpBodyFromParamDictionary:paramDic];
    [NSURLConnection connectionWithRequest:request delegate:self];
}```

//hash算法
- (NSString*) sha1:(NSString *)hashString {
const char *cstr = [hashString cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:hashString.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@”%02x”, digest[i]];
return output;
}

//64位 hash算法
- (NSString ) sha1_base64:(NSString )hashString {
const char *cstr = [hashString cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:hashString.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
base64 = [GTMBase64 encodeData:base64];
NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
return output;
}

  • (NSData )httpBodyFromParamDictionary:(NSDictionary )param {
    NSMutableString * data = [NSMutableString string];
    for (NSString * key in param.allKeys) {
    [data appendFormat:@”%@=%@&”,key,param[key]];
    }
    return [[data substringToIndex:data.length-1] dataUsingEncoding:NSUTF8StringEncoding];
    }



  • (void)connection:(NSURLConnection )connection didReceiveData:(NSData )data
    {
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
    [self loginRongCloud:@”graveyards1215@hotmail.com” token:[dic valueForKey:@”token”] password:@”tomorrow962464an”];

}

  • (void)loginRongCloud:(NSString )userName token:(NSString )token password:(NSString *)password
    {
    //登陆融云服务器
    [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
    NSLog([NSString stringWithFormat:@”token is %@ userId is %@”,token,userId],nil);
    } error:^(RCConnectErrorCode status) {
    //关闭HUD
    NSLog(@”RCConnectErrorCode is %ld”,(long)status);

    } tokenIncorrect:^{
    NSLog(@”IncorrectToken”);

    dispatch_async(dispatch_get_main_queue(), ^{
    });
    

    }];
    }

“`

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

文章标题

AsyncTask连续启动多次时出现的bug

文章标题自动标注程序

  • 2015-06-25 17:04
  • 6.93MB
  • 下载

文章标题

测试编辑器的效果这里写链接内容

文章标题

题目如下:建议使用QMutex QThread 3、生产者消费者:有一个生产者对象,还有一个消费者对象生产者用来向篮子对象中放苹果,每次1个。消费者从篮子中取走吃掉1个苹果,生产苹果时不能吃...

文章标题

android 的进程基本知识当应用程序第一次启动的时候,Android会启动一个Linux 进程和一个主线程,默认情况下,所有组件都会在该进程和线程中运行,Android会为每一个应用程序分配一个单...

Cmake编译安装MySQL完整版

cmake编译安装MySQL

文章标题

## 类的常识 ## struct和class唯一的不同为在struct中未被标明为public,protected,private的默认为public,而class中默认为private,其余完全相...

文章标题

// // ViewController.m // UI-NO-26-多线程-GCD // // Created by 杨政涛 on 15/8/28. // Copyright (c...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)