1
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?

1
  • providing the actual URL you are sending would be helpful.. Commented Jan 27, 2011 at 10:17

7 Answers 7

5

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).

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

You can try this

NSString *sampleUrl = @"http://www.google.com/search.jsp?params=Java Developer"; NSString* encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]; 

Comments

1

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.

Comments

1

It was the problem in UNIX server... It was giving wrong data.

Comments

1
NSString *encodedstring = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)yoururlstring, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8); 

this code works perfect

Comments

0

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.