0

Previously, I was having a problem in posting special characters to web service. But now, I can POST all special characters except "?" which I do not understand why.

NSString+URLEndoding.h

@interface NSString (NSString_URLEncoding) //originally NSOBject
- (NSString *)urlEncodeUsingEncoding:(CFStringEncoding)encoding;
- (NSString *)urlEncode;
@end

NSString+URLEndoding.m

@implementation NSString (NSString_URLEncoding)

- (NSString *)urlEncodeUsingEncoding:(CFStringEncoding)encoding {
return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                 (__bridge CFStringRef)self,
                                                                 NULL,
                                                                 CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                 encoding));
}

- (NSString *)urlEncode {
    return [self urlEncodeUsingEncoding:kCFStringEncodingUTF8];
}

@end

Below are some part of UpdateSomething.m:

    NSString *encodeSomething = [something urlEncode];
    NSMutableString *post = [NSMutableString stringWithFormat:@"something=%@", encodeSomething];
    //NSLog(@"post: %@", post);
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
    //NSLog(@"PostData: %@", postData);
    NSString *check = [[NSString alloc] initWithData:postData encoding:NSASCIIStringEncoding]; //UTF8 also works.
    NSLog(@"check string = %@", check);
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:strURLQueryString]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
    NSURLResponse *response;
    NSError *err=nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
}

The output of check is:

check string = something%20%3F

I know that "%3F" = "?". But I do not understand why I get error whenever I key in question mark.

Response: Input Array does not match ?

Your assistance(s) are appreciated.

Confused_person
  • 103
  • 1
  • 2
  • 11
  • try \\ before question mark – manujmv Aug 19 '13 at 04:34
  • That response is from the server? What makes you think that response has anything to do with the encoded question mark? Can you just have your server echo back the request params, or log them? step 1 is to confirm that the params arrive as you expect them to, step 2 is to make sure that the server does the right thing. I think you're sending the question mark correctly as %3f, but maybe your server doesn't want a question mark. – danh Aug 19 '13 at 05:38
  • I can run the query succesfully on my SQL server. However, when I looked at the scrip, there is this add_escape_custom($something). add_escape_custom() calls this function mysql_real_escape_string(). I thought that was the case, I removed add_escape_custom. But still, unable to key in "?". – Confused_person Aug 19 '13 at 07:31

1 Answers1

0

I tried to do this again some time after I posted this question.

Previously, I used a web service that was written by someone else. I suspect it has something to do with some custom escaping character function.

This time, I wrote my own web service, and it works perfectly. I don't even need to use the "NSString_URLEncoding"

Confused_person
  • 103
  • 1
  • 2
  • 11