• 大小: 16KB
    文件类型: .zip
    金币: 2
    下载: 0 次
    发布日期: 2024-02-06
  • 语言: Python
  • 标签:

资源简介

gzip-encoding-实例,如何压缩 HTTP 请求发送到 web 服务,并处理

资源截图

代码片段和文件信息

#include 
#include 

#include 
#include 

#include “GZipCodec/GZipCodec.h“

using json = nlohmann::json;

static const std::string base_url = “http://127.0.0.1:9000/“;
// static const std::string base_url = “http://127.0.0.1:9000/alt/“;
// static const std::string base_url = “http://127.0.0.1:9000/drf/“;

void upload_plain_json(std::string data_string)
{
    auto response = cpr::Post(cpr::Url{base_url}
                              cpr::Body{data_string}
                              cpr::Header{{“Content-Type“ “application/json“}});

    std::cout << “Response to plain upload:“ << std::endl << response.text << std::endl;
    std::cout << “Status code was: “ << response.status_code << std::endl;
}

void upload_gzip_json(std::string data_string)
{
    std::string compressedData;

    GZipCodec::Compress(data_string compressedData);

    auto response = cpr::Post(cpr::Url{base_url}
                              cpr::Body{compressedData}
                              cpr::Header{{“Content-Type“ “application/json“}
                                          {“Content-Encoding“ “gzip“}});

    std::cout << “Response to gzip upload:“ << std::endl << response.text << std::endl;
    std::cout << “Status code was: “ << response.status_code << std::endl;
}

int main(int argc char* argv[])
{
    json json_data;

    json_data = {
        // from https://en.wikipedia.org/wiki/JSON
        {“firstName“  “John“}
        {“lastName“ “Smith“}
        {“isAlive“ true}
        {“age“ 25}
        {“address“ {
            {“streetAddress“ “21 2nd Street“}
            {“city“ “New York“}
            {“state“ “NY“}
            {“postalCode“ “10021-3100“}
        }}
        {“phoneNumbers“ {
            {
                {“type“ “home“}
                {“number“ “212 555-1234“}
            }
            {
                {“type“ “office“}
                {“number“ “646 555-4567“}
            }
            {
                {“type“ “mobile“}
                {“number“ “123 456-7890“}
            }
        }}
        {“children“ json::array()}
        {“spouse“ NULL}
    };

    upload_plain_json(json_data.dump());
    upload_gzip_json(json_data.dump());

    return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-04-20 05:39  gzip-encoding-master\
     文件         158  2016-04-20 05:39  gzip-encoding-master\.gitmodules
     文件          72  2016-04-20 05:39  gzip-encoding-master\CONTRIBUTORS.md
     文件         421  2016-04-20 05:39  gzip-encoding-master\CREDITS.md
     文件        3248  2016-04-20 05:39  gzip-encoding-master\LICENSE
     文件        2375  2016-04-20 05:39  gzip-encoding-master\README.rst
     目录           0  2016-04-20 05:39  gzip-encoding-master\cpp\
     文件          11  2016-04-20 05:39  gzip-encoding-master\cpp\.gitignore
     目录           0  2016-04-20 05:39  gzip-encoding-master\cpp\GZipCodec\
     文件        1964  2016-04-20 05:39  gzip-encoding-master\cpp\GZipCodec\GZipCodec.cpp
     文件         803  2016-04-20 05:39  gzip-encoding-master\cpp\GZipCodec\GZipCodec.h
     文件        1078  2016-04-20 05:39  gzip-encoding-master\cpp\GZipCodec\LICENSE
     文件         511  2016-04-20 05:39  gzip-encoding-master\cpp\Makefile
     文件         535  2016-04-20 05:39  gzip-encoding-master\cpp\README.rst
     文件        2268  2016-04-20 05:39  gzip-encoding-master\cpp\client.cpp
     目录           0  2016-04-20 05:39  gzip-encoding-master\cpp\cpr\
     目录           0  2016-04-20 05:39  gzip-encoding-master\cpp\json\
     目录           0  2016-04-20 05:39  gzip-encoding-master\cpp\obj\
     文件           0  2016-04-20 05:39  gzip-encoding-master\cpp\obj\.placeholder
     目录           0  2016-04-20 05:39  gzip-encoding-master\django\
     文件         656  2016-04-20 05:39  gzip-encoding-master\django\README.rst
     文件         565  2016-04-20 05:39  gzip-encoding-master\django\drf_views.py
     文件         237  2016-04-20 05:39  gzip-encoding-master\django\manage.py
     文件         863  2016-04-20 05:39  gzip-encoding-master\django\middleware.py
     文件         170  2016-04-20 05:39  gzip-encoding-master\django\requirements.txt
     文件          63  2016-04-20 05:39  gzip-encoding-master\django\run_me
     文件         247  2016-04-20 05:39  gzip-encoding-master\django\settings.py
     文件         340  2016-04-20 05:39  gzip-encoding-master\django\urls.py
     文件         673  2016-04-20 05:39  gzip-encoding-master\django\views.py
     目录           0  2016-04-20 05:39  gzip-encoding-master\flask\
     文件         476  2016-04-20 05:39  gzip-encoding-master\flask\README.rst
............此处省略8个文件信息

评论

共有 条评论

相关资源