C++ URLencode library
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的更多相关文章
- 推薦使用 Microsoft Anti-Cross Site Scripting Library V3.0
原文出至: http://blog.miniasp.com/post/2009/07/29/Recommand-Microsoft-Anti-Cross-Site-Scripting-Library- ...
- python 全栈开发,Day115(urlencode,批量操作,快速搜索,保留原搜索条件,自定义分页,拆分代码)
今日内容前戏 静态字段和字段 先来看下面一段代码 class Foo: x = 1 # 类变量.静态字段.静态属性 def __init__(self): y = 6 # 实例变量.字段.对象属性 # ...
- Robot Framework 教程 (4) - 自定义Library
RobotFrame Work为我们提供了包括OS.Android.XML.FTP.HTTP.DataBase.Appium.AutoIt.Selenium.Watir等大量的库.在使用过程中,除这些 ...
- 《The Python Standard Library》——http模块阅读笔记1
官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...
- urllib.urlencode() 无法encode中文, UnicodeEncodeError
urllib.urlencode() 无法encode中文, UnicodeEncodeError, 具体错误内容如下:File "/System/Library/Frameworks/Py ...
- 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 ...
- 记一个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 ...
- 阿里签名中URLEncode于C#URLEncod不同之处
问题 如上图所示,阿里云的PercentEncode 转换! 为 %21 PercentEncode 源码为: package com.aliyuncs.auth; import java.io.Un ...
- 代码的坏味道(22)——不完美的库类(Incomplete Library Class)
坏味道--不完美的库类(Incomplete Library Class) 特征 当一个类库已经不能满足实际需要时,你就不得不改变这个库(如果这个库是只读的,那就没辙了). 问题原因 许多编程技术都建 ...
随机推荐
- Linux安装npm并打包前端代码
查看node版本$ node -v查看npm版本$ npm -v如果没有安装node及npm,需要先安装node及npm#yum install node# yum install npm安装cnpm ...
- Hbase运维手册(1)
1. region情况 需要检查 1. region的数量(总数和每台regionserver上的region数) 2. region的大小 如果发现异常可以通过手动merge region和手动分配 ...
- jdk7 并行计算框架Fork/Join
故名思义,拆分fork+合并join.jdk1.7整合Fork/Join,性能上有大大提升. 思想:充分利用多核CPU把计算拆分成多个子任务,并行计算,提高CPU利用率大大减少运算时间.有点像,Map ...
- JSON.parse JSON.stringify
JSON.stringify() undefined 值.函数或者XML值会被忽略 数组当中含有 undefined值,函数或XML值,该数组中的这些值将会被当成 null 正则对象会被转成空对象 J ...
- hibernate validator【原】
hibernate validator 功能 在开发中经常做一些字段校验的功能,比如非空,长度限制,邮箱验证等等,为了省掉这种冗长繁琐的操作,hibernate validator提供了一套精简的注释 ...
- OPC和DCOM配置
本文为Java实现OPC通信的一部分 系统:使用win10 64位专业版 PDF文件: 本文,链接: 百度网盘 密码: reht, Win7和Win7_SP1网络OPC配置,链接: 百度网盘 密码 ...
- asp.net mvc cshtml (VIEWS)中怎么提供URL参数:
其实,没有必要,只要在view中这样获取就可以: <%=Html.ViewContext.RouteData.Values["id"]%> 就算没有id的参数也不会报错 ...
- mongoDB与mongoose
mongodb是一个基于分布式文件存储的文档型数据库 MongoDB 是一个介于关系数据库和非关系数据库之间的产品 MongoDB 最大的特点是他支持的查询语言非常强大,而且还支持对数据建立索引 官方 ...
- Python正则匹配之有名分组
参考:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html # re.match import re m = re.match(r'( ...
- 服务器上定时自动执行php
两种方式: 一.Linux 服务器Linux原生支持crontab,所以可以利用这一功能做定时任务 步骤: 1.编辑crontab文件:Linux:crontab -e 2.输入代码:0 0 * * ...