I need a library that can URLencode a string/char array.

Now, I can hex encode an ASCII array like here:http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029

But I need something that works with Unicode. Note: On Linux AND on Windows !

CURL has a quite nice:

 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

2 Answers

//If I read the quest correctly and you want to do this yourself, without using curl I think I have a solution (sssuming UTF-) and I think this is a conformant and portable way of URL encoding query strings:

#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() << std::setfill('') << 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+, 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+) + 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. 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

C++ URLencode library的更多相关文章

  1. 推薦使用 Microsoft Anti-Cross Site Scripting Library V3.0

    原文出至: http://blog.miniasp.com/post/2009/07/29/Recommand-Microsoft-Anti-Cross-Site-Scripting-Library- ...

  2. python 全栈开发,Day115(urlencode,批量操作,快速搜索,保留原搜索条件,自定义分页,拆分代码)

    今日内容前戏 静态字段和字段 先来看下面一段代码 class Foo: x = 1 # 类变量.静态字段.静态属性 def __init__(self): y = 6 # 实例变量.字段.对象属性 # ...

  3. Robot Framework 教程 (4) - 自定义Library

    RobotFrame Work为我们提供了包括OS.Android.XML.FTP.HTTP.DataBase.Appium.AutoIt.Selenium.Watir等大量的库.在使用过程中,除这些 ...

  4. 《The Python Standard Library》——http模块阅读笔记1

    官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...

  5. urllib.urlencode() 无法encode中文, UnicodeEncodeError

    urllib.urlencode() 无法encode中文, UnicodeEncodeError, 具体错误内容如下:File "/System/Library/Frameworks/Py ...

  6. The Java library for converting Wikipedia wikitext notation to HTML

    https://code.google.com/p/gwtwiki/ The Java Wikipedia API (Bliki engine) is a parser library for con ...

  7. 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file

    我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...

  8. 阿里签名中URLEncode于C#URLEncod不同之处

    问题 如上图所示,阿里云的PercentEncode 转换! 为 %21 PercentEncode 源码为: package com.aliyuncs.auth; import java.io.Un ...

  9. 代码的坏味道(22)——不完美的库类(Incomplete Library Class)

    坏味道--不完美的库类(Incomplete Library Class) 特征 当一个类库已经不能满足实际需要时,你就不得不改变这个库(如果这个库是只读的,那就没辙了). 问题原因 许多编程技术都建 ...

随机推荐

  1. python 计算机发展史,线程Process使用 for循环创建 2种传参方式 jion方法 __main__的解释

    ########################总结################## #一 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬 ...

  2. canvas svg webgl threejs d3js 的区别

    canvas 绘制2D位图. Echarts是基于Canvas技术的可视化工具,底层封装了原生的JavaScript的绘图 API. canvas里面绘制的图形不能被引擎抓取,canvas中我们绘制图 ...

  3. linux chmod命令使用

    chmod:更改文件9个属性 Linux文件属性有两种设置方法,一种是数字,一种是符号. Linux文件的基本权限就有九个,分别是owner/group/others三种身份各有自己的read/wri ...

  4. Linux学习笔记:【001】Linux内核分析

    Linux内核 Linux内核是Linux系统构成中最核心的一个部分,是由5个子系统组成. 进程调度: 进程调度(SCHED)控制进程对CPU的访问.当需要选择下一个进程运行时,由调度程序选择最值得运 ...

  5. Java基础高级部分(一)

    1. 集合部分 1.1 HashMap排序 package cn.Douzi.hashMap01; import java.util.ArrayList; import java.util.Colle ...

  6. oldboy s21day04

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 1.简述解释型语言和编译型语言的区别?"""1.解释型语言:Python,PH ...

  7. 记录MySQL的一些基础操作

    MySQL建表操作 root@localhost 08:05:22> create table stu( -> id int(4) not null, -> name char(20 ...

  8. java操作数据库:分页查询

    直接上.... 还是用之前的goods表,增加了一些数据 1.实体类Goods // 封装数据 public class Goods { private int gid; private String ...

  9. electron-vue:Vue.js 开发 Electron 桌面应用

    相信很多同学都知道 Electron 可以帮助开发人员使用前端技术开发桌面客户端应用,今天介绍的 electron-vue 框架是一套基于 Vue.js 开发 Electron 桌面应用的脚手架,该项 ...

  10. tensorflow的特征工程函数

    1. # creates a real valued column for dense numeric data tf.contrib.layers.real_valued_column(    co ...