C-URL编码
有没有一种简单的方法在 C 中进行 URL 编码?我正在使用 libcurl 但我还没有找到方法。具体来说,我需要进行百分比转义。
Is there a simple way to do URL encode in C? I'm using libcurl but I haven't found a method. Specifically, I need to do percent escapes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在C语言中基于维基百科,无需分配和释放。确保输出缓冲区至少是输入 URL 字符串的 3 倍。通常,您只需要编码最多 4K,因为 URL 往往很短,因此只需在堆栈上进行即可。
像这样使用它
In C based on wikipedia, without having to alloc and free. Make sure output buffer is at least 3X input URL string. Usually you only need to encode up to maybe 4K as URLs tend to be short so just do it on the stack.
Use it like this
curl_escape
取代
显然已被
curl_easy_escape
curl_escape
which apparently has been superseded by
curl_easy_escape
我写这个也是为了处理空格字符的查询字符串编码
用法: UrlEncode(" http://www.example.com/index.html?Hello=World", " :/", buffer, buf_size)
url:要编码的 url 字符串。可以是字符串文字或字符串数组
encode:要编码的以零结尾的字符串。这是很好的,因为您可以在运行时确定要编码的 url 大小
buffer:保存新字符串的缓冲区
size:缓冲区的大小
返回:如果缓冲区足够大,则返回新字符串的大小;如果缓冲区不够大,则返回所需的缓冲区大小。如果您想分配所需的确切大小,可以双击此功能。
这个函数几乎可以处理您需要的大多数(如果不是全部)url 编码。最重要的是 - 它真的很快!
I wrote this to also take care of query string encoding of the space character
Usage: UrlEncode("http://www.example.com/index.html?Hello=World", " :/", buffer, buf_size)
url: The url string to encode. Can be string literal or string array
encode: A zero-terminated string of chars to encode. This is good cause you can at runtime determine how much of the url to encode
buffer: A buffer to hold the new string
size: The size of the buffer
return: Returns the size of the new string if buffer is large enough OR returns the required buffer size if the buffer is not large enough. You can double tap this function if you want to allocate the exact size needed.
This function pretty much handles most (if not all) url encoding you'd need. Best of all - it's really fast!