NSURL带有花括号 [英] NSURL with Curly Braces
问题描述
NSURL不支持在网址中使用花括号(即{})。我的应用程序需要与需要在URL中使用花括号的服务器通信。我正在考虑使用桥接器以Python或C ++编写网络代码,甚至考虑重写NSURL的C代码以使其接受花括号。我的远程服务器不接受百分比转义。
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?
编辑:解释为什么addPercentEscapesUsingEncoding等在这里对我不起作用。
Explained why addingPercentEscapesUsingEncoding and the like don't work for me here.
推荐答案
如果你逃避括号,它会对你有用吗?
Will it work for you if you escape the braces?
此代码:
// 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);
输出:
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)
因此,转义似乎有效
以下是您可以通过编程方式转义网址的方法:
Here's how you can programmatically escape the url:
NSString *escapedUrlString = [unescaped stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
编辑:
在下面的评论中,你说你的服务器不会接受编码的大括号,还有其他选择。首先,我会尝试修复服务器。如果那是不可能的......我没有用括号等尝试过这个......但是NS网络类下面的层是CFNetworking。
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.
看到这个:
来自该文档:
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.
这篇关于NSURL带有花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!