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 - 關於資訊的單獨⺴⽹網絡請求 ----結束----php

相關文章
相關標籤/搜索