2014-07-22 47 views
1

比方說,這是工作的捲曲發送十六進制字符串:作爲數據服務器使用AFNetworking 2

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

其中「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; 
} 
+0

提供您盡最大努力的代碼以及任何錯誤消息和失敗的描述。 – zaph

+0

編輯問題與我如何嘗試的例子。 – dormitkon

回答

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輸出:

數據(十六進制):< 7b227375 63636573 73223a74 7275657d>
data(string):{「success」:true}

注意:通過任何方式使用AFNetworking,太棒了!我只是知道較老的OSX/iOS方法,而且我沒有安裝AFNetworking的項目。

相關問題