user3266139
user3266139

Reputation: 41

Http Post parameters to url don't send special characters "+" in objectC Xcode

I have create an application that send two POST parameters to aspx server and it's save data in database. It's an Iphone Application.

Here's the code:

        NSString *post = [NSString stringWithFormat:@"name=%@&number=%@", 
        name,number];
        NSString *capturedpost = [post 
        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *postData = [capturedpost dataUsingEncoding:NSUTF8StringEncoding];
        //NSString *postLength = [NSString stringWithFormat:@"%d", [postData 
        length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:@"http:/www.myurl.aspx"]];
        [request setHTTPMethod:@"POST"];
        //[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        //[request setValue:@"application/x-www-form-urlencoded"
 forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        NSURLResponse *requestResponse;
        NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request
     returningResponse:&requestResponse error:nil];
        NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler 
    bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
        NSLog(@"requestReply: %@", requestReply);
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
    returningResponse:nil error:nil];

But on server, the string "number" that contain the telephone number like "+393333..." save in db the number without the "+". How can I do? The server side works fine, because the same App on Android that do same request work perfect!

Upvotes: 0

Views: 853

Answers (1)

user3266139
user3266139

Reputation: 41

OK, thanks to "holex" I successfully encode the URL in this way:

 NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                       NULL,
                                                                                       (CFStringRef)post,
                                                                                       NULL,
                                                                                       (CFStringRef)@"+",
                                                                                       kCFStringEncodingUTF8 ));

And the "+" saved correctly in db!

Upvotes: 3

Related Questions