Base64 Over HTTP POST丢失数据(Objective-C) [英] Base64 Over HTTP POST losing data (Objective-C)
问题描述
我目前有一个HTTP POST请求和一个Base64编码库,我将我的图像编码为B64,然后通过POST方法通过HTTP发送它。
I currently have a HTTP POST Request and a Base64 Encoding Library, I encode my image to B64 then send it over HTTP via the POST method.
我输出了Base64到XCodes控制台,复制并粘贴它,它完美地工作。虽然数据库中存储的Base64 I(MongoDB,纯文本文件等)总是在另一端损坏。
I output the Base64 to XCodes console, copy and paste it and it works perfectly. Although the Base64 I store within the Database (MongoDB, Plain Text File etc) always comes out corrupt on the other end.
工作版本(从XCode复制并粘贴): http://dontpanicrabbit.com/api/working.php
破碎版(来自MongoDB数据库):< a href =http://dontpanicrabbit.com/api/grabimage.php> http://dontpanicrabbit.com/api/grabimage.php
Working Version (Copied and Pasted from XCode): http://dontpanicrabbit.com/api/working.php Broken Version (From MongoDB Database): http://dontpanicrabbit.com/api/grabimage.php
如果您查看源代码,您会发现它们是相同的,但是在破碎的版本中添加了空格。
If you view the source you'll notice they are the same but there is added whitespace into the broken version.
我使用的Objective-C代码是:
The Objective-C code I am using is:
MyImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(MyImage.image, 0);
[Base64 initialize];
NSString *encoded = [Base64 encode:imageData];
NSString *urlPOST = encoded;
//NSLog(@"%@",encoded);
NSString *varyingString1 = @"picture=";
NSString *varyingString2 = urlPOST;
NSString *post = [NSString stringWithFormat: @"%@%@", varyingString1, varyingString2];
NSLog(@"%@", post);
//NSString *post = @"image=%@",urlPOST;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"url/api/insertimage.php"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *strResult = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
PHP - > MongoDB存储
PHP -> MongoDB Storage
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->dablia;
// access collection
$collection = $db->images;
// insert a new document
$item = array(
'picture' => $_POST['picture']
);
$collection->insert($item);
echo 'Inserted document with ID: ' . $item['_id'];
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
输出代码:
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->dablia;
// access collection
$collection = $db->images;
// execute query
// retrieve all documents
$cursor = $collection->find();
// iterate through the result set
// print each document
foreach ($cursor as $obj) {
echo '<img src="data:image/jpeg;base64,'.trim($obj['picture']).'">';
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
我不知道为什么我似乎在破坏POST?
I have no idea why I seem to be corrupting over POST?
推荐答案
问题正是我在第一次评论中提出的问题。也就是说,base64编码数据可以包含+字符。在x-www-form-urlencoded数据中,接收器知道'+'是空格字符的编码。因此,由于您不是对base64值进行URL编码,因此任何+实例都会导致数据在收到时被破坏。
The problem is exactly what I suggested in my first comment. That is, base64 encoded data can contain '+' characters. In x-www-form-urlencoded data the receiver knows that '+' is an encoding of a space character. Thus since you aren't URL encoding your base64 value, any instances of '+' will cause the data to be corrupted when received.
您的'+'字符初始数据在接收和存储时变为''。当您输出该值时,它是无效的base64编码数据。
The '+' characters in your initial data are turning into ' ' when received and stored. When you then output that value, it is invalid base64 encoded data.
如果您检查工作与非工作示例的来源,您将看到空白存在于原始Base64编码值中存在+的位置。您看到的任何换行都是因为您查看源代码的是将字符包裹在''字符处。
If you examine the source of your working vs. non-working examples you'll see that the whitespace exists EXACTLY where there is a '+' in the original Base64 encoded value. Any newlines you're seeing are because whatever you're viewing the source in is wrapping lines at a ' ' character.
在您的iOS代码中,您需要正确编码base64编码值,在你的情况下,你真正需要做的就是对'+'字符进行百分比编码。
In your iOS code you need to properly encode the base64 encoded value, in your case all you really need to do is percent encode the '+' characters.
编辑添加,以回应评论:
EDIT to add, in response to comment:
post = [post stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
这篇关于Base64 Over HTTP POST丢失数据(Objective-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!