开发者

How to encode an url

开发者 https://www.devze.com 2023-02-07 04:12 出处:网络
NSString* urlEncode(NSString * url) { string inStr = StringFromNSString(url); CFStringRef inStringRef = CFStringCreateWithCString( kCFAllocatorDefault, inStr.c_str(), kCFStringEncodingUTF8 );
NSString* urlEncode(NSString * url)
{
    string inStr = StringFromNSString(url);
    CFStringRef inStringRef = CFStringCreateWithCString( kCFAllocatorDefault, inStr.c_str(), kCFStringEncodingUTF8 );
    NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)inStringRef,NULL,(CFStringR开发者_如何学编程ef)@"!*’();:@&=+$,/?%#[]",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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号