iphone:检索后的 nsdata 编码 - 为什么这么奇怪?

发布于 2024-11-08 03:40:41 字数 3328 浏览 0 评论 0原文

我当前的应用程序开发有一个奇怪的问题。我的应用程序的目标是通过 NSURLSConnection(是的,没有 ASIHTTPRequest 框架)接收多个 xhtml 网站,将它们保存在数组中,并通过 NSURLConnection 将它们发布到 web 服务,该服务为我解析一些信息。

问题:我遇到了非常奇怪的编码问题,并尝试了很多解决方法。

接收 xhtml 网站:

 (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];}

之后我处理数据并希望将其发送到网络服务。因此我做了一些准备工作:

方法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])];
}

方法2:

NSMutableString *data = [[NSMutableString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

方法3:

CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [lastSearchParam.dataObjectsEncoding objectForKey:key]);
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
NSMutableString *data = [[NSMutableString alloc] initWithData:theData encoding: encoding];

方法4:

NSString *data = [[NSString alloc] initWithBytes:theData length:[theData length] encoding:NSUTF8StringEncoding];

方法5:

NSString *data2 = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

在我添加了一些东西之后,我想将收到的XHTML HTTP POST到Web服务。为了对整个参数和接收到的数据进行编码,我将所有内容添加到一个大字符串中,并且

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

大多数网站都采用 UTF-8。所以我首先尝试了“方法2”。如果我 NSLog 接收到的整个数据,一切看起来都很好,我可以查看正确的 HTML 源代码,甚至 € 符号也正确显示。但是如果我将 HTTP POST 传输到 Web 服务,则 Web 服务不会收到所有 XHTML。一些特殊字符似乎会破坏传输,例如“;”符号。

所以我在互联网上搜索并想出了“方法1”,将收到的xhtml网站解析为ASCII并进行一些自制的特殊字符对话 - 这对大多数标志都有效,但例如“€”不起作用并且不正确由网络服务接收。但在这种情况下,一切都转移到了网络服务 - 但遗憾的是字符错误。

下一次尝试是“方法3”,我在接收网站时保存了网站的编码,并在以后使用该信息。但这里有与 UTF8 编码相同的问题:传输被一些特殊字符中断...

“方法 4”和“方法 5”也不起作用...

问题:为什么在我的 HTTP POST 期间传输中断到网络服务?

I have a strange problem with 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文