NSURL does not support the use of curly braces (i.e. {}) in URLs. My application needs to talk to a server that requires the use of curly braces in the URL. I'm considering using bridges to write the networking code in Python or C++, or even rewriting the C code for NSURL to make it accept curly braces. Percent escapes are not accepted by my remote server.
Do I have any other good alternatives?
EDIT: Explained why addingPercentEscapesUsingEncoding and the like don't work for me here.
Will it work for you if you escape the braces?
This code:
// escape {} with %7B and %7D
NSURL *url = [NSURL URLWithString:@"http://somesite.com/%7B7B643FB915-845C-4A76-A071-677D62157FE07D%7D.htm"];
NSLog(@"%@", url);
// {} don't work and return null
NSURL *badurl = [NSURL URLWithString:@"http://somesite.com/{7B643FB915-845C-4A76-A071-677D62157FE07D}.htm"];
NSLog(@"%@", badurl);
Outputs:
2011-11-30 21:25:06.655 Craplet[48922:707] http://somesite.com/%7B7B643FB915-845C-4A76-A071-677D62157FE07D%7D.htm
2011-11-30 21:25:06.665 Craplet[48922:707] (null)
So, escaping seems to work
Here's how you can programmatically escape the url:
NSString *escapedUrlString = [unescaped stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
EDIT:
In your comment below, you said your server won't accept encoded braces and was there any other alternatives. First, I would try and get the server fixed. If that's not possible ... I haven't tried this with braces etc... but the layer below NS networking classes is CFNetworking.
See this:
From that doc:
CFStringRef url = CFSTR("http://www.apple.com");
CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);
CFStringRef requestMethod = CFSTR("GET");
....
Once again, haven't tried it and I'm running out. might try it later but if a layer isn't working your first options are to move down the stack.
Bryan's answer is perfectly great (+1 to him!), but another solution would be to use NSString's stringByAddingPercentEscapesUsingEncoding:
method.
Like this:
NSString * originalURLAsString = [NSString stringWithString: @"http://somesite.com/{7B643FB915-845C-4A76-A071-677D62157FE07D}.htm"];
NSString * properlyEncodedString = [originalURLAsString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL * url = [NSURL URLWithString: properlyEncodedString];
I hope this info helps you out!
To call using CFNetwork can use the follwoing
-(void)getDataFromUrl{
CFStringRef tempURLA = CFSTR("http://my.test.server/iostest/index.html?{\"a\":\"b\"}");
CFStringRef tempUrlSting = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)tempURLA,CFSTR("{}"), CFSTR("\""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, tempUrlSting, NULL);
CFStringRef requestMethod = CFSTR("GET");
CFHTTPMessageRef myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myURL,kCFHTTPVersion1_1);
CFStringRef headerFieldName = CFSTR("Accept");
CFStringRef headerFieldValue = CFSTR("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
CFHTTPMessageSetHeaderFieldValue(myRequest, headerFieldName, headerFieldValue);
[self performHTTPRequest:myRequest];
}
-(void)performHTTPRequest:(CFHTTPMessageRef)request {
CFURLRef gotdatab = (__bridge CFURLRef)(CFBridgingRelease(CFHTTPMessageCopyRequestURL(request)));
// NSLog(@"(CFHTTPMessageRef request %@",gotdatab);
CFReadStreamRef requestStream = CFReadStreamCreateForHTTPRequest(NULL, request);
CFReadStreamOpen(requestStream);
NSMutableData *responseBytes = [NSMutableData data];
NSError *error;
while (TRUE) {
if (CFReadStreamHasBytesAvailable(requestStream)) {
UInt8 streambuffer[1024];
int readBytes = CFReadStreamRead (requestStream,streambuffer,sizeof(streambuffer));
NSLog(@"Read: %d",readBytes);
[responseBytes appendBytes:streambuffer length:readBytes];
}
if (CFReadStreamGetStatus(requestStream) == kCFStreamStatusError) {
error = (NSError*)CFBridgingRelease(CFReadStreamCopyError (requestStream));
if ([error code] == 61) {
// connection refused
NSLog(@"Error occured: %d",[error code]);
}
break;
}
if (CFReadStreamGetStatus(requestStream) == kCFStreamStatusAtEnd) {
NSLog(@"Stream reached end!");
error = nil;
break;
}
}//
CFHTTPMessageRef response = (CFHTTPMessageRef)CFReadStreamCopyProperty(requestStream, kCFStreamPropertyHTTPResponseHeader);
if (response==NULL) {
NSLog(@"response is null");
return;
}
}
The above was done using examples from here and here
But even the above method has the same issue. That is: if {} are not encoded the URL doesn't get generated. If the {} are encoded the server doesn't return a proper value.
Any other suggestions to resolve this posts issue?
来源:https://stackoverflow.com/questions/8335796/nsurl-with-curly-braces