所以我们在同一页上:我正在使用NSData+GZIP、 JSONKit 和NSData+Base64
正如标题所暗示的那样,我的目标是获取一个 json 对象,对其进行压缩,对其进行 base64 编码,然后将其作为表单发送到 .net 服务器,而后者又希望获取这些数据并反转过程并结束带有一个 json 对象。
我需要在 android 和 iOS 上都这样做。android 搞定了,很好用。作为参考,这是我在那里做的事情:
public String compress(String string) throws IOException {
byte[] blockcopy = ByteBuffer.allocate(4).order(java.nio.ByteOrder.LITTLE_ENDIAN).putInt(string.length()).array();
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
os.close();
byte[] compressed = new byte[4 + os.toByteArray().length];
System.arraycopy(blockcopy, 0, compressed, 0, 4);
System.arraycopy(os.toByteArray(), 0, compressed, 4, os.toByteArray().length);
StringBuffer hexString = new StringBuffer();
for (int i=0; i<compressed.length; i++)
hexString.append(Integer.toHexString(0xFF & compressed[i]));
return Base64.encodeBytes(compressed);
}
这是在 iOS 上成功的测试:
NSLog(@"test? %@", [[NSString alloc]initWithData:[[NSData dataFromBase64String:[[[@"test" dataUsingEncoding:NSUTF8StringEncoding] gzippedData] base64EncodedString]] gunzippedData] encoding:NSUTF8StringEncoding]);
这打印:
"test? test"
关注领域:
NSStringEncoding encoding = NSUTF8StringEncoding;
NSString* string = [[NSArray arrayWithArray:mutableData] JSONString];
NSData* data = [string dataUsingEncoding:encoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/myendpoint.aspx?deviceid=%@&accesstoken=%@", [defaults objectForKey:@"deviceId"], [defaults objectForKey:@"ggAccessToken"]]]];
[request setHTTPMethod:@"POST"];
NSMutableData *requestBodyData = nil;
requestBodyData = [NSMutableData data];
// Create boundry
NSString *boundary = [NSString stringWithFormat:@"--%@--", [[NSProcessInfo processInfo] globallyUniqueString]];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
NSData *boundaryData = [[[@"--" stringByAppendingString:boundary] stringByAppendingString:@"\r\n"] dataUsingEncoding:encoding];
// Add Data to POST
[requestBodyData appendData:boundaryData];
[requestBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: multipart/form-data; "] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"name=\"%@\"; ", @"contacts"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n"] dataUsingEncoding:encoding]];
NSString* finalString = [[data gzipDeflate] base64EncodedString];
//an example of finalString at this point: H4sIAAAAAAAAA6WOwQrCMAyGX0VyUqjHOtk7CN5HGekWMdB2knansXc3FcSDKIiHhBC+5P+6BWYJGdrOGbhdp0R1XiCgpwAtnCbPgcBAmqMn0c3WWrvbaNtrWVjNC+ZzffARPsCqIRSRn4EXllz6hJGULcK5YNJzHEehnB8qrgb8o9j8onj8rjigCNObobsD+2WV/kYBAAA=
NSData* finalData = [finalString dataUsingEncoding:NSUTF8StringEncoding];
[requestBodyData appendData:finalData];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:encoding]];
[requestBodyData appendData:boundaryData];
[request setHTTPBody:requestBodyData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"%@", [data objectFromJSONData]);
}];
此时一切似乎都很好,但来自(.net)服务器的结果:
ERROR TYPE: System.IO.InvalidDataException - ERROR MESSAGE: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. -
我意识到这里还没有太多问题,但这里有一个问题:此时我可以做什么会产生不正确的压缩数据?(假设此时完全是正确的服务器端,因为 android 功能齐全,并且构建它的那部分的人非常有经验)......