iphone: nsdata encoding after retrieval - why so strange?
I have a strange problem with开发者_Python百科 my current app development. the aim of my app is to receive several xhtml websites via NSURLSConnection (yes, no ASIHTTPRequest framework), save them in an array and post them via NSURLConnection to a webservice, which parses some information for me.
problem: I have really strange encoding problems and tried a lot of workarounds.
receiving of xhtml website:
(void)connection:(CustomURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [response textEncodingName]);
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
[lastSearch.dataObjectsEncoding setObject:[response textEncodingName] forKey:connection.tag ];
[receivedData setLength:0];}
afterwards I work with the data and want to send it over to the webservice. therefor I do some preparations:
method 1:
NSMutableString *data = [[NSMutableString alloc] initWithData:theData encoding:NSASCIIStringEncoding];
NSArray *escapeChars = [NSArray arrayWithObjects: @"€", @"\n", @";" , @"/" , @"?" , @":" ,
@"@" , @"&" , @"=" , @"+" ,
@"$" , @"," , @"[" , @"]",
@"#", @"!", @"'", @"(",
@")", @"*", nil];
NSArray *replaceChars = [NSArray arrayWithObjects: @"%E2%82%AC", @"", @"%3B" , @"%2F" , @"%3F" ,
@"%3A" , @"%40" , @"%26" ,
@"%3D" , @"%2B" , @"%24" ,
@"%2C" , @"%5B" , @"%5D",
@"%23", @"%21", @"%27",
@"%28", @"%29", @"%2A", nil];
int len = [escapeChars count];
int i;
for(i = 0; i < len; i++)
{
[data replaceOccurrencesOfString: [escapeChars objectAtIndex:i]
withString:[replaceChars objectAtIndex:i]
options:NSLiteralSearch
range:NSMakeRange(0, [data length])];
}
method 2:
NSMutableString *data = [[NSMutableString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
method 3:
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [lastSearchParam.dataObjectsEncoding objectForKey:key]);
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
NSMutableString *data = [[NSMutableString alloc] initWithData:theData encoding: encoding];
method 4:
NSString *data = [[NSString alloc] initWithBytes:theData length:[theData length] encoding:NSUTF8StringEncoding];
method 5:
NSString *data2 = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
after I added some stuff I want to HTTP POST the received XHTML to the webservice. to encode the whole parameters and the received data I add everything to a big string an do a
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
most websites are in UTF-8. so I first tried "method 2". if I NSLog the whole received data, everything seems fine, I can view the correct HTML source code, even the € sign is displayed correctly. but if I transmit the HTTP POST to the webservice, there is not all XHTML received by the web service. some special chars seems to break the transmission, e.g. a ";" sign.
so I searched the internet and came up with "method 1", parsing the received xhtml websites to ASCII and do some selfmade special char conversation - this worked for most signs, but e.g. the "€" didn't work and wasn't correctly received by the web service. but in this case, everything got over to the webservice - but sadly with wrong chars.
next try was "method 3", I saved the encoding of the websites while receiving them and use that information later on. but here was the same problem as with UTF8 encoding: the transmission was breaking by some special chars...
"method 4" and "method 5" didn't work as well..
question: why does the transmission breaks during my HTTP POST to the web service?
精彩评论