NSString* urlEncode(NSString * url)
{
string inStr = StringFromNSString(url);
CFStringRef inStringRef = CFStringCreateWithCString( kCFAllocatorDefault, inStr.c_str(), kCFStringEncodingUTF8 );
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)inStringRef,NULL,(CFStringRef)#"!*’();:#&=+$,/?%#[]",kCFStringEncodingUTF8 );
return encodedString;
}
I am using above method to encode url... even though my app is crashing saying
<body>
<div id="content">
<h1>An Error Was Encountered</h1>
<p>The URI you submitted has disallowed characters.</p> </div>
</body>
</html>
terminate called after throwing an instance of 'std::invalid_argument'
what():
Any idea.. What is wrong with my code?
FYI: It is crashing in this method JSONNode jsonObject0 = libJSON::parse( inResponseData );
UPDATED: The server which i am sending message is UNIX server is it causing problem?
You don't need to create the inStr or inStringRef temporary variables. The types NSString* and CFStringRef are "toll free bridge" types. These types are interchangable with just a simple cast.
You can find more information about toll free bridging here: http://www.mikeash.com/pyblog/friday-qa-2010-01-22-toll-free-bridging-internals.html
That said, you can simplify your method to just the following:
-(NSString*) urlEncode
{
NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'();:#&=+$,/?%#[]", kCFStringEncodingUTF8 );
return [encodedString autorelease];
}
The above is best implemented as an NSString category (note the use of self as the value to encode).
This works fine for me. I use CFSTR instead of (CFStringRef)#"!*’();:#&=+$,/?%#[]"
- (NSString *)encodedURLParameterString {
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR(":/=,!$& '()*+;[]##?"),
kCFStringEncodingUTF8);
return [result autorelease];
}
You can try this
NSString *sampleUrl = #"http://www.google.com/search.jsp?params=Java Developer";
NSString* encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
You need to make sure that you're not URL encoding more than you need. If you're going to be using this encoding string as part of an actual URL, then you should know that there are only portions of the URL that are supposed to be encoded, namely the query string (there are other bits as well, but 95% of the time, this has to do with the query).
In other words, your URL should be:
scheme://host/path?<key>=<value>&<key>=<value>
In this, ONLY the stuff inside the angle brackets (<key> and <value>) should be URL encoded.
It was the problem in UNIX server... It was giving wrong data.
NSString *encodedstring = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)yoururlstring,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8);
this code works perfect
Here is the best way to encode the formatted URLString:
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Instead of using below as it has some memory management issues.
NSString *encodedstring = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)yoururlstring,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8);
Related
I'm doing this to encode my URL in this way,
but its not working,
i got the result in NSLog but its the same url nothing is changing.
Please help me to sort this issue.
below is my code :
NSString *unencodedUrlString =
[#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#" %#", unencodedUrlString);
Thanks in advance
The comma is a legal URL character, therefore stringByAddingPercentEscapesUsingEncoding leaves "2,7" as it is and does not replace it by "2%2C7".
If you want the comma to be replaced by a percent escape (as I understand from your
comment to the question), you can use CFURLCreateStringByAddingPercentEscapes
instead:
NSString *str = #"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7";
NSString *encoded = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef)(str), NULL, CFSTR(","), kCFStringEncodingUTF8));
NSLog(#"%#", encoded);
Output:
http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7
The fourth parameter CFSTR(",") specifies that the comma should be replaced by
a percent escape even if it is a legal URL character.
Use this
NSString *str = [NSString stringWithFormat:#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"];
NSString *path = [str stringByReplacingOccurrencesOfString:#"," withString:#"/"];
NSLog(#"%#",path);
This will do nothing but will make , to /.
I am trying to decode some text sent from a server that is URLEncoded, and this is the best method I managed to find.
I use these standard functions for URLEncododing:
NSString* encodeToPercentEscapeString(NSString *string) {
return (__bridge NSString *)
CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef) string,
NULL,
(CFStringRef) #"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8);
}
NSString* decodeFromPercentEscapeString(NSString *string) {
return (__bridge NSString *)
CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
(__bridge CFStringRef) string,
CFSTR(""),
kCFStringEncodingUTF8);
}
If I encode special characters:
NSString* encoded = encodeToPercentEscapeString(#"ąśżźćęółń");
I get #"%C4%85%C5%9B%C5%BC%C5%BA%C4%87%C4%99%C3%B3%C5%82%C5%84"
which is ok.
But if I try to decode it:
NSString* original = decodeFromPercentEscapeString(encoded);
I get :#"ńÖŇõŇľŇļńáńô√≥ŇāŇĄ".
Why?
Is there a better method to decode URLEncoded text?
Have you tried -[NSString stringByReplacingPercentEscapesUsingEncoding:]?
Also, you are using the bridging casts incorrectly. The created strings will leak, because ARC doesn't know it owns them. Use CFBridgingRelease(). It's nice because it is a variant of CFRelease() which is normally necessary to balance a Core Foundation "Create" function, but it cooperates with ARC.
Really quick question that is driving me INSANE. I was wondering if someone could tell me why this line is leaking?
NSString *post = [NSString stringWithFormat:#"<someXML><tagWithVar=%#></tagWithVar></someXML>",var];
post = [NSString stringWithFormat:#"xmlValue=%#",(NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)post,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 )];
I am just encoding a string into a URL format. From my understanding, stringWithFormat: should return an autoreleased object. Apparently that is not the case. It works, but leaks. Any ideas??
You are using the method CFURLCreateStringByAddingPercentEscapes. If a Core Foundation function has "Create" in its name, it means that you own the returned object. In other words, you'll need to release the CFStringRef returned by CFURLCreateStringByAddingPercentEscapes.
NSString *post = [NSString stringWithFormat:#"...", var];
CFStringRef stringRef = CFURLCreateStringByAddingPercentEscapes(...);
post = [NSString stringWithFormat:#"xmlValue=%#",(NSString *)stringRef];
CFRelease(stringRef);
Have a slight problem. Trying to post XML to a server. To do this, I have to encode the XML string in URL format.
NSString *encodedString = [NSString stringWithFormat:#"xmlValue=%#",[post stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]];
That's what I have and it works for all the characters EXCEPT the '='. This does not get converted to %3D. Has anyone else ever had this problem before? Am I specifying the wrong encoding type? I have tried "NSUTF8StringEncoding" as well.
This is a little piece of the XML string:
#"<xml-service application=\"broadcast\" type=\"REQUEST\"><identity token=\"xxxxxxxx\".....
Any help would be appreciated! Thanks
This should solve your problem:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 );
Source
You can try this. I found this helpful.
NSString *sampleUrl = #"http://www.google.com/search.jsp?params=Java Developer";
NSString* encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
The following code works, but it's ugly and creates a bunch of autoreleased objects. I'm using similar code for parsing reserved HTML characters as well (for quotes, & symbols, etc). I'm just wondering... Is there a cleaner way?
NSString *result = [[NSString alloc] initWithString:userInput];
NSString *result2 = [result stringByReplacingOccurrencesOfString:#"#"
withString:#"\%23"];
NSString *result3 = [result2 stringByReplacingOccurrencesOfString:#" "
withString:#"\%20"];
formatted = [[result3 stringByReplacingOccurrencesOfString:#"&"
withString:#"\%26"] retain];
[result release];
#Yannick, you were on the right track, thank you. stringByAddingPercentEscapesUsingEncoding sort of works but ignores certain characters that can still be a problem (like slashes). Here is the best way to do URL encoding:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 );
Have you tried to use the stringByAddingPercentEscapesUsingEncoding method?
formatted = [result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];