C++ C++;URL编码库(支持Unicode)?

C++ C++;URL编码库(支持Unicode)?,c++,windows,linux,C++,Windows,Linux,我需要一个可以对字符串/字符数组进行URL编码的库 现在,我可以对ASCII数组进行十六进制编码,如下所示: 但我需要一些与Unicode兼容的东西。 注意:在Linux和Windows上 CURL有一个非常好的功能: char *encodedURL = curl_easy_escape(handle,WEBPAGE_URL, strlen(WEBPAGE_URL)); 但首先它需要卷曲,它也不是Unicode的能力,正如StruLn < P>你可以考虑首先将Unicode URL转换

我需要一个可以对字符串/字符数组进行URL编码的库

现在,我可以对ASCII数组进行十六进制编码,如下所示:

但我需要一些与Unicode兼容的东西。 注意:在Linux和Windows上

CURL有一个非常好的功能:

 char *encodedURL = curl_easy_escape(handle,WEBPAGE_URL, strlen(WEBPAGE_URL));

但首先它需要卷曲,它也不是Unicode的能力,正如StruLn

< P>你可以考虑首先将Unicode URL转换成UTF8,UTF8数据将携带你的Unicode数据在ASCII字符中,一旦你的URL是UTF8格式,你就可以很容易地用你喜欢的API对URL进行编码。

如果我正确地阅读了这个任务,你想自己做这件事,而不使用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;
}

UTF-8是传输unicode数据的有线协议之一。它还有一个额外的优点,就是向后兼容ASCII编码+1代表GJ的建议。@maxschlepzig:Unicode根据MSFT=UTF-16,根据Linux上的wchar\u t Unicode=UTF-32;)
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));
}
http://foo.com/bar?abc<>de??90   210fg!"$%
http://google.com
http://www.unicode.com/example?großpösna
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