libcurl url decode 如何实现
时间: 2024-05-12 22:18:53 浏览: 7
在libcurl中,可以使用curl_easy_unescape()函数进行URL解码。该函数的原型如下:
```
char *curl_easy_unescape(CURL *handle, const char *string, int length, int *outlength);
```
其中,handle是curl_easy_init()函数返回的句柄,string是需要解码的URL字符串,length是字符串的长度,outlength是解码后字符串的长度。该函数返回解码后的字符串。
示例代码:
```c
#include <curl/curl.h>
#include <stdio.h>
int main() {
CURL *curl = curl_easy_init();
if(curl) {
const char *url = "http%3A%2F%2Fwww.example.com%2Ftest%3Fid%3D123%26name%3D%E5%BC%A0%E4%B8%89";
int len = strlen(url);
int outlen;
char *decoded_url = curl_easy_unescape(curl, url, len, &outlen);
if(decoded_url) {
printf("%s\n", decoded_url);
curl_free(decoded_url);
}
curl_easy_cleanup(curl);
}
return 0;
}
```
输出结果:
```
http://www.example.com/test?id=123&name=张三
```