22 Star 67 Fork 23

xiaozhuai / cxxurl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

CXXCurl

[English README] [中文说明]

CXXUrl 是基于libcurl的c++库,但它不仅仅只是一个curl的面向对象的c++封装

最新版本的CXXUrl将只包含头文件

如果你想了解更多关于curl的信息,访问 http://curl.haxx.se/

Under MIT LICENSE

作者:xiaozhuai - xiaozhuai7@gmail.com

构建

使用cmake构建项目

$ cd /path/to/cxxurl
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make check # 运行测试用例

运行示例

构建完成后,你就可以运行一些示例了

$ ../example_server/start_server # 为运行示例开启一个http服务, 需要 php > 5.6
$ ./example_get

安装

直接将 src/cxxurl 中的文件复制到你需要的地方!

例子和用法

你可以在example目录下找到所有例子的源码

Get

See examples/example_get.cpp

要发起一个get请求并获取输出,这是所有你需要做的事情

至于解析body,我们提供了一个json解析器

See examples/example_parse_json_body.cpp

感谢 https://github.com/nlohmann/json

你可以简单的将 contentOutput 改为一个 ofstream 类的实例就可以完成下载的功能.

See examples/example_download.cpp

如果你需要响应头的输出

See examples/example_response_header.cpp

当然,我们还提供了一个解析器

See examples/example_parse_response_header.cpp

Post

发起一个post请求或许比get要复杂一些,但仍然是一件非常简单的事情

See examples/example_post.cpp

CXXUrl 提供了三者form类型 SimpleForm, MultipartFormRawRequestBody.

  • SimpleForm ---- 仅仅是键值对 (application/x-www-form-urlencoded)
  • MultipartForm ---- 文件和键值对 (multipart/form-data)
  • RawRequestBody ---- 二进制数据 (raw request body)

Https

See examples/example_get_https_enable.cpp

对于某些网站,你也可以关闭ssl验证,像这样 .verifySSL(false)

See examples/example_get_https_disable.cpp

设置Request Headers

设置Request Headers用法类似于form

See examples/example_set_request_header.cpp

对于一些常用的header, 例如userAgent、 referer等,你可以通过这些方法设置 .userAgent(...), .referer(...)

Cookies

再来说说cookie

假设一种情景,我们需要登录一个网站,把登录成功的cookie保存到一个文件,然后下次访问的时候带上这个cookie

See examples/example_cookie.cpp

原生CURL选项

如果你想要使用原生的CURL选项

See examples/example_use_curl_option.cpp

这个例子和第一个例子(get)是完全等效的, 不同之处是使用了 .curlOptionString(CURLOPT_URL, "http://xiaozhuai.github.io") 来替代 .url("http://xiaozhuai.github.io")

RequestBuilder

RequestBuilder正如它的名字,仅仅是个builder,它只是为了简化代码,你完全可以不使用它

See examples/example_without_request_builder.cpp

那么你喜欢哪种方式呢?

最后

感谢cURL

MIT License Copyright (c) 2016 xiaozhuai, Weihang Ding Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

C++ http库,不仅仅只是一个curl的面向对象的C++封装 展开 收起
C++ 等 5 种语言
MIT
取消

发行版 (3)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/xiaozhuai/cxxurl.git
git@gitee.com:xiaozhuai/cxxurl.git
xiaozhuai
cxxurl
cxxurl
master

搜索帮助