用Obj-C发送一个放大器(&) [英] Sending an amp (&) using a post with Obj-C
问题描述
page.php?text = Hello World&空间。
现在我发现&被服务器接收,但读取好像一个新的变量启动。所以看到(我想):
text =Hello World
空格。 =
我读过,我可以尝试对文本进行编码,使其看起来像一个URL(像[space]变成%20),但没有办法正确编码。我得出结论:
textToPOST = [text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
但是并不对&符号进行编码,而是对其他任何东西进行编码。所以结果是:
some text& uuml; blablabla
变成
一些%20text%20& uuml;%20blablabla
没有编码。那么我该怎么做,请帮忙。
非常感谢
要在XML中显示和&符号,只需使用& amp; amp; amp;引用。与纯粹的&不同,NSXMLParser不会输出错误。使用& amp; amp还允许您进行其他符号转义,如:
- ü [在文中] - >& uuml; [在HTML] - >& amp; amp; uuml; [in XML]
- Ä [在文中] - >& Auml; [在HTML] - >& amp; amp; amp; amp; amp; amp; [in XML]
- ß [在文中] - >& szlig; [在HTML] - >& amp; amp; szlig; [in XML]
- 等...
所以这就是解决 em>如何使用放大器解析XML问题。此外,我设法使代码,从我链接到我的原始问题的评论的网站,工作:
NSMutableString * deutschEscaped = [NSMutableString stringWithString:[[deutschTextLabel string] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[deutschEscaped replaceOccurrencesOfString:@$withString:@%24选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@& withString:@%26选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@+withString:@%2B选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@,withString:@%2C选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@/withString:@%2F选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@:withString:@%3A选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@; withString:@%3B选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@=withString:@%3D选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@? withString:@%3F选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@@withString:@%40选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@withString:@%20选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@\twithString:@%09选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@#withString:@%23选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@< withString:@%3C选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@> withString:@%3E选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@\withString:@%22选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@\\\
withString:@%0A选项:NSCaseInsensitiveSearch范围:NSMakeRange(0,[deutschEscaped length])];
这对我来说可以正常工作,并且取代了&符号和任何其他可能的危险。
如果您不希望每个空间都有许多%20转义,您还可以使用:
NSMutableString * englishEscaped = [NSMutableString stringWithString:[[englishTextLabel string] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
谢谢大家:)
I am sending a post containing text, numbers and data. The numbers and data work fine, but I'm having problems with the text, since it may contain an ampersand (&). For example
page.php?text=Hello World & Space.
Now I found that the "&" is received by server, but read as if a new variable starts. So it sees (I think):
text = "Hello World "
Space. =
I did read that I could try to encode the text to make it look like it's a URL (like " " [space] turns into "%20") but there is no way to encode it properly. I came to the conclusion:
textToPOST = [text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
But that does NOT encode the ampersands, but everything else. So the result is:
some text ü blablabla
turns into
some%20text%20ü%20blablabla
with the & not encoded. So how can I do this, please help.
Thanks a lot already
Thanks for that answer and sorry for my premature posting :S I hope it at least helps anybody who has the same issue as I had. I just had a chat with a friend and he told me:
To display and ampersand in XML simply use the & quote. Unlike with a pure "&", the NSXMLParser won't output an error. Using & also allows you to make other symbol escapes like:
- ü [in text] -> ü [in HTML] -> &uuml; [in XML]
- Ä [in text] -> Ä [in HTML] -> &Auml; [in XML]
- ß [in text] -> ß [in HTML] -> &szlig; [in XML]
- etc...
So that's how you solve the "How do I parse XML with an amp in it problem". Also I managed to make the code, from the website I linked in a comment to my original question, work:
NSMutableString *deutschEscaped = [NSMutableString stringWithString:[[deutschTextLabel string] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[deutschEscaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
This works fine for me and does replace ampersands and any other possible hazards.
If you don't want many %20 escapes for every space, you can also use:
NSMutableString *englishEscaped = [NSMutableString stringWithString:[[englishTextLabel string] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Thank you all :)
这篇关于用Obj-C发送一个放大器(&)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!