URL Encoding - CFURLCreateStringByAddingPercentEscapes
来源:互联网 发布:泛普软件 编辑:程序博客网 时间:2024/05/16 08:52
转自: http://objcolumnist.com/2009/10/25/escaping-a-url/
URL Encoding
If you have tried to send any information using a GET web request, you would have come across an annoying problem. That annoying problem is making sure that the URL is correctly encoded.
At first glance it would seem that the Cocoa Frameworks do this for you, and you would be right …. well kind of.
The issue is that by default most of these methods leave characters such as & = ? within a URL, as they are strictly speaking valid. The problem is that these characters have special meanings in a GET request, and will more than likely make your request in valid.
Luckily there is a function in Core Foundation that helps:
What makes this function useful, is the legalURLCharactersToBeEscaped parameter. This will escape legal characters such as & ? = if they are supplied. This allows you to escape parameters using the following code.
An example of when to use this, is Twitters Update status API. You can find that here http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0update
To update your status to the following:
This is my status
You would need to post up the following URL:
http://twitter.com/statuses/update.xml?status=This%20is%20my%20status
As this is such a common problem of mine, I have created a category on NSURL. This allows you to pass in a base URL and a parameters dictionary.
Using a parameters dictionary keeps the code nice and clean, but beware, to use the category method above you still have to make sure that your keys, and the base URL are correctly encoded (no spaces or invalid characters !!!!!).
As we now have a category method to do all the hard work for us, to create the Twitter URL you just need to do the following:
And thats it. Obviously this category can be used for things other than twitter ….. if you really want to.
- URL Encoding - CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes[转]
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- CFURLCreateStringByAddingPercentEscapes
- CFURLCreateStringByAddingPercentEscapes
- CFURLCreateStringByAddingPercentEscapes
- URL Encoding
- URL encoding
- URL Encoding
- URL encoding
- url encoding
- IOS开发之CFURLCreateStringByAddingPercentEscapes--URL 编码
- 抵制现实的诱惑,保持心灵的宁静
- 自定义消息示例程序,转自孙鑫笔记
- POJ 1321 棋盘问题 DFS
- Java3d 开抗锯齿
- 如何在互联网云中建立自己的私有空间
- URL Encoding - CFURLCreateStringByAddingPercentEscapes
- Select总结(一)
- Android退出应用程序方法
- Breezy 的Partner Network计划,让你通过手机连接周边打印机
- 关于sftp连接的问题.
- 黑马程序员-交通灯项目的思考
- java反射机制
- CMD
- U盘(硬盘)安装Ubuntu 10.10 Alternate&Desktop