使用AFNetworking 2将十六进制字符串作为数据发送到服务器

时间:2014-07-22 15:35:44

标签: ios objective-c afnetworking afnetworking-2

让我们说这是有效的CURL:

curl -s -d 726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561 http://support.questprojects.com/portal/bukd.aspx?action=LOGIN

Where" 726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561"是十六进制字符串如何使用AFNetworking 2.0实现这一目标?尝试了一切,但没有成功......

我尽力而为:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

NSString *url = [NSString stringWithFormat:@"%@?action=LOGIN", @"http://support.questprojects.com/portal/bukd.aspx"];

NSData *postBody = [self base64DataFromString:@"726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561"];
[manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:postBody name:@"" fileName:@"" mimeType:@""];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];



- (NSData *)nsdataFromHexString: (NSString *)string
{
    NSString *command = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableData *commandToSend= [[NSMutableData alloc] init];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    for (int i = 0; i < ([command length] / 2); i++) {
        byte_chars[0] = [command characterAtIndex:i*2];
        byte_chars[1] = [command characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [commandToSend appendBytes:&whole_byte length:1];
    }
    return commandToSend;
}

1 个答案:

答案 0 :(得分:1)

不要Base64编码十六进制字符串,只需转换为NSData

NSString *hexString = @"726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561";
NSData *postData = [hexString dataUsingEncoding:NSUTF8StringEncoding];

POST数据不需要是Base64编码。

这是一个完整的工作示例,但没有使用AFNetworking,因为我不熟悉它:

NSString *hexString = @"726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561";
NSData *postData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://support.questprojects.com/portal/bukd.aspx?action=LOGIN"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPBody:postData];
[urlRequest setHTTPMethod:@"POST"];
NSURLResponse *urlResponse;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];

NSLog(@"data (hex): %@", data);
NSLog(@"data (string): %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

NSLog输出:

  

数据(十六进制):&lt; 7b227375 63636573 73223a74 7275657d&gt;

  data(string):{&#34; success&#34;:true}

注意:一定要使用AFNetworking,这太棒了!我只是熟悉旧的OSX / iOS方法,而且我没有安装AFNetworking的项目。