首页 > 解决方案 > 如何使用 libcurl 将大字符串发送到乌鸦服务器?

问题描述

这是我的 libcurl 请求(curl_example.cpp)

#include <iostream>
#include <string>
#include <curl/curl.h>
#include <vector>
#include <fstream>      // std::ifstream
using namespace std;
using namespace cv;
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(void)
{
    CURL *curl;
    CURLcode res;
    std::string readBuffer;
    int length=80000;
    char c = 'z';
    string encoded_png=std::string(length, c);
    std::string url_data="http://0.0.0.0:18080/params?query="+encoded_png;
    struct curl_slist *headerlist=NULL;
    curl = curl_easy_init();
    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
        curl_easy_setopt(curl, CURLOPT_URL,url_data.c_str()) ;
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        std::cout <<"readBuffer:"<< readBuffer << std::endl;
    }
    return 0;
}

这是我的 Crow C++ 服务器代码 (crow_example.cpp)

#include<iostream>
#include "crow.h"
#include<string>
using namespace std;
int main()
{

    crow::SimpleApp app;
        CROW_ROUTE(app, "/params")
    ([](const crow::request& req){
        std::ostringstream os;

        // To get a simple string from the url params
        // To see it in action /params?foo='blabla'
        os << "Params: " << req.url_params << "\n\n"; 
        os << "The string received is " << (req.url_params.get("query") == nullptr ? "not " : "") << "found.\n";
                return crow::response{os.str()};
    });    
    app.port(18080).multithreaded().run();
}

长度=80000;当我运行 crow c++ 服务器并使用 libcurl 查询服务器时,我可以在 crow 中接收字符串并在 curl 中获得响应。

长度 = 85000;.//超过 80,000 我无法在 crow 服务器上接收任何字符串,也没有来自 libcurl 请求的响应。

使用 python 烧瓶服务器,我可以接收任意长度的字符串。但是乌鸦服务器不工作,我无法弄清楚根本原因是什么。

任何帮助或意见表示赞赏。

标签: c++curllibcurlcrow

解决方案


推荐阅读