【问题标题】:C++ URLencode library (Unicode capable)?C++ URLencode 库(支持 Unicode)?
【发布时间】:2010-08-28 07:50:45
【问题描述】:

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

现在,我可以对一个 ASCII 数组进行十六进制编码,如下所示: http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029

但我需要一些适用于 Unicode 的东西。 注意:在 Linux 和 Windows 上!

CURL 有一个相当不错的:

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

但首先,它需要 CURL,而且它也不支持 unicode,正如 strlen 所看到的那样

【问题讨论】:

    标签: c++ windows linux


    【解决方案1】:

    如果我正确阅读了任务并且您想自己执行此操作,而不使用 curl,我想我有一个解决方案(sssuming 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;
    }
    

    它没有除了 boost 之外的非标准依赖项,如果你不喜欢 boost 依赖项,删除它并不难。

    我使用以下方法对其进行了测试:

    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
    

    这些examples与哪个正方形

    【讨论】:

      【解决方案2】:

      您可以考虑先将您的 Unicode URL 转换为 UTF8,UTF8 数据会以 ASCII 字符携带您的 Unicode 数据,一旦您获得 UTF8 格式的 URL,您就可以轻松地使用您喜欢的 API 对 URL 进行编码。

      【讨论】:

      • UTF-8 是传输 unicode 数据的有线协议之一。它具有向后兼容 ASCII 编码的额外优势。为 GJ 的建议 +1。
      • @maxschlepzig:根据 MSFT 的 Unicode = UTF-16,根据 Linux 上的 wchar_t Unicode = UTF-32 ;)
      猜你喜欢
      • 2011-06-09
      • 2021-04-08
      • 1970-01-01
      • 2011-03-10
      • 2013-06-23
      • 2017-09-14
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      相关资源
      最近更新 更多