Let's say this is the CURL which works:
curl -s -d 726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561 http://support.questprojects.com/portal/bukd.aspx?action=LOGIN
Where "726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561" is hex string. How to make this with AFNetworking 2.0? Tried everything, but without success...
My best effort:
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;
}
Do not Base64 encode the hex string, just convert to NSData
:
NSString *hexString = @"726240786C722E6E6574FE3765653035356338636463616236323263383065353339616461643963643561";
NSData *postData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
POST data does not need to be Base64 encoded.
Here is a complete working example but not using AFNetworking because I am not conversant in it:
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 output:
data (hex): <7b227375 63636573 73223a74 7275657d>
data (string): {"success":true}
Note: By all means use AFNetworking, it is great! I just know the older OSX/iOS methods by heart and I did not have a project that already had AFNetworking installed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.