最近项目要调用快递鸟查询快递信息,但是快递鸟的demo只有java .net php 三个版本,
还好懂一点java ,自己翻译了一下官方demo,翻译的时候涉及到了C基础,以及MD5和base64知识
有使用的可以直接使用下面的code:( java ——> OC)
.h
//
// BWExpressInfo.h
// BookWay
//
// Created by 陶亚利 on 2016/12/7.
// Copyright © 2016年 陶亚利. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BWExpressInfo : NSObject
+ (void)getExpressInfo:(NSDictionary*)parameters block:(void (^) (NSInteger sucess,NSDictionary *dataDict,NSString *error))block;
@end
.m
//
// BWExpressInfo.m
// BookWay
//
// Created by 陶亚利 on 2016/12/7.
// Copyright © 2016年 陶亚利. All rights reserved.
//
#import "BWExpressInfo.h"
#import <UIKit/UIKit.h>
#import <CommonCrypto/CommonDigest.h>
#import "AFNetworking.h"
#define CC_MD5_DIGEST_LENGTH 32
@implementation BWExpressInfo
static char base64EncodeChars[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/' };
+ (void)getExpressInfo:(NSDictionary *)parameters block:(void (^)(NSInteger, NSDictionary *, NSString *))block {
// 快递鸟 注册时的字段 填写你的app对应的字段
NSString *EBusinessID = @"XXXXXXX";
NSString *AppKey = @"XXXXXXXXXXXX";
// 快递鸟 快递信息查询时不同的类型对应不同的链接
NSString *ReqURL = @"http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
NSString *requestStr = [NSString stringWithFormat:@"{'OrderCode':'','ShipperCode':'%@','LogisticCode':'%@'}",parameters[@"expCode"],parameters[@"expNo"]];
// NSLog(@"requestDataString = %@",requestStr);
NSString *encodePath = [BWExpressInfo urlEncoder:requestStr];
NSString *dataSign = [BWExpressInfo encrypt:requestStr keyValue:AppKey charset:@"UTF-8"];
NSString *dataSignEncoder = [BWExpressInfo urlEncoder:dataSign];
NSDictionary *requestParameters = @{@"RequestData":encodePath,@"EBusinessID":EBusinessID,@"RequestType":@"1002",@"DataSign":dataSignEncoder,@"DataType":@"2"};
[BWExpressInfo PostExpressInfo:ReqURL params:requestParameters block:^(NSInteger sucess, NSDictionary *dataDict, NSString *error) {
if (sucess == 0) {
block(0,dataDict,nil);
} else{
block(1,dataDict,nil);
}
}];
}
+ (void)PostExpressInfo:(NSString *)urlString params:(NSDictionary *)parameters block:(void (^) (NSInteger sucess, NSDictionary *dataDict, NSString *error))block{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
if (block) {
block(0, responseObject,nil);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
block(1,nil,nil);
}];
}
+ (NSData*)JSONData:(NSDictionary *)dict{
NSError* error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
if ([jsonData length] > 0 && error == nil){
return jsonData;
}else{
return nil;
}
}
+ (NSString *)urlEncoder:(NSString *)urlStr{
NSString *encodePath = nil;
if ([[UIDevice currentDevice].systemVersion floatValue] > 7.0) {
encodePath = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}else{
encodePath = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
}
return encodePath;
}
+ (NSString *) encrypt:(NSString *)content keyValue:(NSString *)keyValue charset:(NSString *)charset{
if (keyValue != nil) {
return [BWExpressInfo base64:[BWExpressInfo MD5:[NSString stringWithFormat:@"%@%@",content,keyValue] charset:charset] charset:charset];
}
return [BWExpressInfo base64:[BWExpressInfo MD5:content charset:charset] charset:charset];
}
+ (NSString *)base64:(NSString *)str charset:(NSString *)charset{
return [BWExpressInfo base64Encode:str];
}
+ (NSString *)base64Encode:(NSString *)dataStr{
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
Byte *dataBuffer = (Byte*)[data bytes];
NSMutableString *resultStr = [NSMutableString string];
NSInteger len = [data length];
NSInteger i = 0;
NSInteger b1,b2,b3;
while (i < len) {
b1 = dataBuffer[i++] & 0xff;
if (i==len) {
// char c = base64EncodeChars[b1 >> 2];
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[b1 >> 2]]];
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[(b1 & 0x3) << 4]]];
[resultStr appendString:@"=="];
break;
}
b2 = dataBuffer[i++] & 0xff;
if (i==len) {
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[b1 >> 2]]];
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >> 4)]]];
//NSLog(@"b2 resultStr == %@",resultStr);
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[(b2 & 0x0f) << 2]]];
[resultStr appendString:@"="];
break;
}
b3 = dataBuffer[i++] & 0xff;
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[b1 >> 2]]];
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >> 4)]]];
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >> 6)]]];
[resultStr appendString:[NSString stringWithFormat:@"%c",base64EncodeChars[b3 & 0x3f]]];
}
//NSLog(@"base64Encode == %@",resultStr);
return resultStr;
}
+ (NSString *)MD5:(NSString *)str charset:(NSString *)charset{
const char *original_str = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(original_str, strlen(original_str), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i++){
[hash appendFormat:@"%02X", result[i]];
}
//NSLog(@"MD5 == %@",[hash lowercaseString]);
return [hash lowercaseString];
}
@end