windows C++ URLencode 库(支持 Unicode)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3589936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ URLencode library (Unicode capable)?
提问by Stefan Steiger
I need a library that can URLencode a string/char array.
我需要一个可以对字符串/字符数组进行 URLencode 的库。
Now, I can hex encode an ASCII array like here: http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029
现在,我可以像这里一样对 ASCII 数组进行十六进制编码:http: //www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029
But I need something that works with Unicode. Note: On LinuxAND on Windows !
但我需要一些适用于 Unicode 的东西。注意:在Linux和 Windows 上!
CURL has a quite nice:
CURL 有一个相当不错的:
char *encodedURL = curl_easy_escape(handle,WEBPAGE_URL, strlen(WEBPAGE_URL));
but first, that needs CURL and it also is not unicode capable, as one sees by strlen
但首先,它需要 CURL 并且它也不支持 unicode,正如 strlen 所见
回答by Flexo
If I read the quest correctly and you want to do this yourself, without using curl I think I have a solution (sssuming UTF-8) and I thinkthis is a conformant and portable way of URL encoding query strings:
如果我正确阅读了任务并且您想自己执行此操作,而无需使用 curl,我想我有一个解决方案(假设 UTF-8),并且我认为这是一种符合且可移植的 URL 编码查询字符串方式:
#include <boost/function_output_iterator.hpp>
#include <boost/bind.hpp>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iterator>
#include <iomanip>
namespace {
std::string encimpl(std::string::value_type v) {
if (isalnum(v))
return std::string()+v;
std::ostringstream enc;
enc << '%' << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << int(static_cast<unsigned char>(v));
return enc.str();
}
}
std::string urlencode(const std::string& url) {
// Find the start of the query string
const std::string::const_iterator start = std::find(url.begin(), url.end(), '?');
// If there isn't one there's nothing to do!
if (start == url.end())
return url;
// store the modified query string
std::string qstr;
std::transform(start+1, url.end(),
// Append the transform result to qstr
boost::make_function_output_iterator(boost::bind(static_cast<std::string& (std::string::*)(const std::string&)>(&std::string::append),&qstr,_1)),
encimpl);
return std::string(url.begin(), start+1) + qstr;
}
It has no non-standard dependencies other than boost and if you don't like the boost dependency it's not that hard to remove.
除了 boost 之外,它没有任何非标准的依赖项,如果你不喜欢 boost 依赖项,它并不难删除。
I tested it using:
我使用以下方法对其进行了测试:
int main() {
const char *testurls[] = {"http://foo.com/bar?abc<>de??90 210fg!\"$%",
"http://google.com",
"http://www.unicode.com/example?gro?p?sna"};
std::copy(testurls, &testurls[sizeof(testurls)/sizeof(*testurls)],
std::ostream_iterator<std::string>(std::cout,"\n"));
std::cout << "encode as: " << std::endl;
std::transform(testurls, &testurls[sizeof(testurls)/sizeof(*testurls)],
std::ostream_iterator<std::string>(std::cout,"\n"),
std::ptr_fun(urlencode));
}
Which all seemed to work:
这一切似乎都有效:
http://foo.com/bar?abc<>de??90 210fg!"$%
http://google.com
http://www.unicode.com/example?gro?p?sna
Becomes:
变成:
http://foo.com/bar?abc%3C%3Ede%3F%3F90%20%20%20210fg%21%22%24%25
http://google.com
http://www.unicode.com/example?gro%C3%9Fp%C3%B6sna
Which squares with these examples
哪些正方形与这些例子
回答by GJ.
You can consider converting your Unicode URL to UTF8 first, the UTF8 data will carry your Unicode data in ASCII characters, Once you get your URL in UTF8 you can easily encode the URL with the API you prefer.
您可以考虑先将您的 Unicode URL 转换为 UTF8,UTF8 数据将以 ASCII 字符携带您的 Unicode 数据,一旦您获得 UTF8 格式的 URL,您就可以轻松地使用您喜欢的 API 对 URL 进行编码。