Sending an amp (&) using a post with Obj-C
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. 开发者_StackOverflow中文版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
Unfortunately stringByAddingPercentEscapesUsingEncoding
is not URL-encoding as most people understand it. It escapes characters that are completely invalid to have in a URL, but not characters that have a special meaning in a URL. So it's usable for ‘correcting’ an invalid user-inputted URL, but it's quite unsuitable for creating a URL from escaped path and query string components.
(In JavaScript terms, it is analogous to encodeURI
and not the one you need far more often which is encodeURIComponent
.)
So to do it properly you will need another method, either your own or pinched from an existing framework (this is a very common sore point so many libraries have workarounds). It's possible to implement using CFURLCreateStringByAddingPercentEscapes
. See this answer for example code.
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] -> ü [in XML]
- Ä [in text] -> Ä [in HTML] -> Ä [in XML]
- ß [in text] -> ß [in HTML] -> ß [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 :)
精彩评论