Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableURLRequest replaced '+' with space

I'm sending request to server NSMutableURLRequest as below, the problem one of the parameter which is a phone number and have '+' sign, is removed.

// Create the request.
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:myURL ]
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                        timeoutInterval:30.0];
    /**
     add post
     ****/
    [theRequest  setHTTPMethod:@"POST"];

    NSString *encodedfullNumberFinal= [ fullNumberFinal stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *append2URL=[NSString stringWithFormat:@"deviceType=%d&token=%@&mobileNumber=%@&code=%@",
                          1, token,

                          encodedfullNumberFinal
                          ,myBid
                          ];



    [theRequest setHTTPBody:[append2URL

                             dataUsingEncoding:NSUTF8StringEncoding /*NSUnicodeStringEncoding*/]];


    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = /*[*/[NSMutableData data] /* retain]*/;
    } else {
        // Inform the user that the connection failed.

    }

Notice, form server side I'm using Tomcat and Java.
any advice how to preserve the parameter with the '+' sign?

like image 310
user836026 Avatar asked Dec 06 '25 07:12

user836026


2 Answers

The problem is that the standard stringByAddingPercentEscapesUsingEncoding doesn't do anything with plus signs. The Core Foundation CFURLCreateStringByAddingPercentEscapes gives you more control, though. And I use a NSString category that provides a simple interface to CFURLCreateStringByAddingPercentEscapes:

@implementation NSString (UrlEncode)

+ (NSString *)stringByAddingPercentEscapesFor:(NSString *)string
                legalURLCharactersToBeEscaped:(NSString *)legalURLCharactersToBeEscaped
                                usingEncoding:(NSStringEncoding)encoding
{
    return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                     (CFStringRef)string,
                                                                     NULL,
                                                                     (CFStringRef)legalURLCharactersToBeEscaped,
                                                                     CFStringConvertNSStringEncodingToEncoding(encoding)
                                                                     ));
}

- (NSString *)stringByAddingPercentEscapesFor:(NSString *)legalURLCharactersToBeEscaped
                                usingEncoding:(NSStringEncoding)encoding
{
    return [NSString stringByAddingPercentEscapesFor:self
                       legalURLCharactersToBeEscaped:legalURLCharactersToBeEscaped
                                       usingEncoding:encoding];
}

- (NSString *)stringByAddingPercentEscapesForURLParameterUsingEncoding:(NSStringEncoding)encoding
{
    return [NSString stringByAddingPercentEscapesFor:self
                       legalURLCharactersToBeEscaped:@":/?@!$&'()*+,;="
                                       usingEncoding:encoding];
}

@end

I can then use stringByAddingPercentEscapesForURLParameterUsingEncoding rather than stringByAddingPercentEscapesUsingEncoding, and the plus sign will be escaped, too.

like image 81
Rob Avatar answered Dec 07 '25 20:12

Rob


Yap, in URLs, + indicates a space. You have to be explicit about including that in the list of characters to be encoded.

NSString *encodedfullNumberFinal = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL,
    (__bridge CFStringRef)fullNumberFinal,
    CFSTR(""),
    CFSTR("+"), // you may want to include more valid URL characters here too
    kCFStringEncodingUTF8,
);

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!