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) 特征 当一个类库已经不能满足实际需要时,你就不得不改变这个库(如果这个库是只读的,那就没辙了). 问题原因 许多编程技术都建 ...
随机推荐
- 面向对象【day07】:析构函数(六)
二.析构函数 一.概述 析构函数,第一次听说这个函数的名称,那这个函数到底是干嘛的呢?什么才是析构函数呐? 定义:在实例销毁的时候调用的函数 二.析构函数定义 2.1 定义 1 2 3 4 5 6 7 ...
- LInux Zebra
本文章摘自linux公社 https://www.linuxidc.com/Linux/2015-07/120224p1.htm 写在前面 从22号中午开始琢磨zebra/quagga的用法,一直 ...
- spring-data-redis集成ehcache实现缓存
1.结构 2.pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns=&qu ...
- Golang入门教程(十四)结构体和类详解
golang中并没有明确的面向对象的说法,实在要扯上的话,可以将struct比作其它语言中的class. 类声明 type Book struct { Title string Author stri ...
- vue使用字体图标转码问题
使用iconfont字体之后,出现的是一个方框,而没有出现相应的图标 需要通过String.fromCharCode方法转成指定unicode编码对应的十六进制字符 <div class=&qu ...
- 使用wget命令下载JDK失败(文件特别小)
问题RT: 我们在网页上下载的时候要点一下 “Accept License Agreement ” ,使用wget下载的时候也需要提交这个 accept,方法如下: wget --no-check-c ...
- [Android] Android 异步定时任务实现的三种方法(以SeekBar的进度自动实现为例)
[Android] Android 定时异步任务实现的三种方法(以SeekBar的进度自动实现为例) 一.采用Handler与线程的sleep(long)方法 二.采用Handler与timer及Ti ...
- springboot-01 helloworld
第一个springboot程序 新建maven项目,添加如下依赖: <?xml version="1.0" encoding="UTF-8"?> & ...
- JS算法练习四
JS算法练习 1.将使用空格分隔单词使用驼峰命名连接起来: var str="HELLO world welcome to my hometown"; /*--先输入一个有空格分隔 ...
- DNN网络(一)
摘自 https://www.cnblogs.com/pinard/p/6418668.html 一.DNN 简介 1.DNN的层次可以分为三层 输入层(input layer) 隐藏层(hidden ...