Question

I'm trying to pass a request to a URL using HTTP POST on iPhone. The HTTP body contains some escape characters.

NSString *requestMessage=[NSString stringWithString:@"?username/u001password/u001description"];
    NSMutableURLRequest *url=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://welcome.com"]];
    [url setHTTPMethod:@"POST"];
    [url setHTTPBody:[requestMessage dataUsingEncoding:NSUTF8StringEncoding]];
     NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:url delegate:self];

The escape character here is /u001.

Using this code I don't get any correct responses. I think the trouble is with the escape characters only. Please give me a solution for how to give an escape sequence like this in Cocoa. Thanks in advance.

Was it helpful?

Solution

You've confused forward slashes (/) with backslashes (\). You need a backslash to form an escape sequence; a slash is just a slash, and “/u001” is just a slash, the letter u, two digits zero, and a digit one.

That said, if you actually want to include U+0001 in your string, even \u001 is wrong. You want \x01 or maybe \u0001 (but I seem to remember that GCC complains if you use \u for a character lower than U+0100).

I do wonder why the server would require U+0001 as the separator, though. Are there public API docs for whatever server you're querying?

OTHER TIPS

What do you want to escape? I don't quite understand what you are trying to do. Do you want to write "&"? Then do it. It's not HTML.

Besides that, [NSString stringWithString:@"…constant string…"] is superfluid. @"…constant string…" is all you need.

There is a method in NSString to add percent-escapes to URLs: -(void)stringByAddingPercentEscapesUsingEncoding:. Maybe that's what you're looking for?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top