MD5

MD5用的包#import <CommonCrypto/CommonDigest.h>


MD5用的包#import <CommonCrypto/CommonDigest.h>
//将做好的字符串进⾏行MD5加密/*

%02X",

此处需要在我们已经写好的MD5类中添加⽅方法
+ (NSString *)md5Digest:(NSString *)str

{ const char *cStr = [str UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5( cStr, strlen(cStr), result); return [[NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X
result[0], result[1], result[2], result[3], result[4], result[5],result[6], result[7], result[8], result[9], result[10], result[11], result[12],result[13], result[14], result[15] ] uppercaseString]; }
copy过去即可,别忘记再.h⽂文件中添加⽅方法声明*/

xxixiaxia'm下面j就bbubu's不是的l了。



//资讯列表接⼝口测试
//url = http://zixun.www.net.cn/api/hichinaapp.php (参数method=list)

//此接⼝口为GET提交接⼝口 参数在排序后,全部以"&"连接/*

基本参数:

ver,client,method(list),module(news),type(类型),pageIndex(⻚页码),pageSize(新闻数量),timestamp(时间戳),appid(分配的appid),appkey(分配的appkey)

type:@"updated"(最新),@"industry"(⾏行业),@"application"(应⽤用),@"value"(价值),@"security"(安全)

appid: @"IOS259" appkey: @"qetzcb259"
timestamp: 其他api接⼝口的trid参数 去掉token和"_" 剩下那个时间作为时间戳*/

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"1.0",@"ver", @"ios",@"client", @"list",@"method", @"news",@"module", @"updated",@"type", @"1",@"pageIndex", @"20",@"pageSize", [self timestamp],@"timestamp", @"IOS259",@"appid", @"qetzcb259",@"appkey",nil];
//参数按⾸首字⺟母排序

NSArray *array = [[params allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; NSLog(@"%@",array);
//按照参数排序后的顺序,将其对应的value,进⾏行字符串拼接NSMutableString *signStr = [[NSMutableString alloc] init];for (int i = 0; i < [array count]; i++) {

NSString *key = [array objectAtIndex:i]; //从排序好的参数数组中,取得key

NSString *value = [params valueForKey:key]; //从做好的参数字典中,通过key,取得对应的value

[signStr insertString:value atIndex:signStr.length]; //将取出的value,加到字符串⾥里⾯面

} NSLog(@"%@",signStr);
//将做好的字符串进⾏行MD5加密/*


%02X",

此处需要在我们已经写好的MD5类中添加⽅方法
+ (NSString *)md5Digest:(NSString *)str

{ const char *cStr = [str UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5( cStr, strlen(cStr), result); return [[NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X
result[0], result[1], result[2], result[3], result[4], result[5],result[6], result[7], result[8], result[9], result[10], result[11], result[12],result[13], result[14], result[15] ] uppercaseString]; }
copy过去即可,别忘记再.h⽂文件中添加⽅方法声明*/


NSString *sign = [MD5 md5Digest:signStr];
//将所有的参数和值进⾏行最后的拼接,使⽤用"&"连接NSArray *keys = [params allKeys];NSArray *values = [params allValues];

NSMutableString *paramsStr = [[NSMutableString alloc] init]; for (int i = 0; i < [keys count]; i++) { NSString *key = [keys objectAtIndex:i]; NSString *value = [values objectAtIndex:i]; [paramsStr appendString:key]; [paramsStr appendString:@"="]; [paramsStr appendString:value]; [paramsStr appendString:@"&"]; } [paramsStr appendFormat:@"sign=%@",sign]; NSLog(@"%@",paramsStr); NSString *urlStr = [NSString stringWithFormat:@"http://zixun.www.net.cn/api/hichinaapp.php?%@",paramsStr]; NSURL *url = [NSURL URLWithString:urlStr]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection connectionWithRequest:request delegate:self];
以下为connection的代理⽅方法

#pragma mark - 关于资讯的单独⺴⽹网络请求 ----开始----
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

self.receiveData = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[self.receiveData appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

NSMutableString *receiveStr = [[NSMutableString alloc]initWithData:self.receiveData encoding:NSUTF8StringEncoding];
//******此处开始 对json字符串进⾏行修改NSRange ra;
ra = [receiveStr rangeOfString:@"{"];NSRange delRa;
delRa.location = 0;
if (ra.length == 0)
{

delRa.length = 0; }
else

{

delRa.length = ra.location; }
[receiveStr deleteCharactersInRange:delRa];//******修改结束

//转化成可⽤用的字典



/*由于此处接收到的data,并不是标准的json格式,所以需要对其进⾏行修改*/

NSDictionary *dic = (NSDictionary *)[receiveStrmutableObjectFromJSONString]; NSLog(@"%@",dic);}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error
{

NSLog(@"%@",[error debugDescription]);}
#pragma mark - 关于资讯的单独⺴⽹网络请求 ----结束----

 
posted @ 2015-09-21 10:48  E山猫  阅读(214)  评论(0编辑  收藏  举报