简体   繁体   English

C++ Curl post 请求不起作用且没有错误

[英]C++ Curl post request not working and no error

I'm trying to do a simple command line programs for doing a post request to another computer on the same network.我正在尝试做一个简单的命令行程序,用于向同一网络上的另一台计算机发出发布请求。 On this computer I will read the message with Hercules, and later I will think at how to read it from c++ code.在这台电脑上我会用 Hercules 读取消息,然后我会考虑如何从 C++ 代码中读取它。 I' m using QtCreator with mingw x64.我正在使用带有 mingw x64 的 QtCreator。

When I run the small project Qt open the console but I see immediately the console that says "press return to close the window".当我运行小项目 Qt 时,打开控制台,但我立即看到控制台显示“按返回关闭窗口”。 What's wrong with the code?代码有什么问题?

#include <iostream>
#include <curl/curl.h>
#include <string>
#include <cstdio>
#include <cstring>
#include <functional>

using namespace std;


class ScopeExit {
public:
    ScopeExit(std::function<void()> f): f_(f) {}
    ~ScopeExit() { f_(); }
private:
    std::function<void()> f_;
};

struct PostData
{
    const char *ptr;
    size_t size;
};

size_t read_data(void* ptr, size_t size, size_t nmemb, void *userp)
{
    PostData *post_data = (PostData*)userp;

    size_t byte_len = size * nmemb;
    if(post_data->size < byte_len)
    {
        byte_len = post_data->size;
    }

    memcpy(ptr, post_data->ptr, byte_len);
    post_data->ptr += byte_len;
    post_data->size -= byte_len;

    return byte_len;
}


static size_t WriteCallback(void *contents, size_t size, size_t nmeb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmeb);
    return size * nmeb;
}

void post_request(std::string ipPort,std::string message)
{
    // EXCECUTION START HERE
    std::cout<<"Setting up request data!"<<std::endl;
    CURL *curl = curl_easy_init();
    if(!curl)
    {
        std::cout<<"curl_easy_init() failed"<<std::endl;
    }


    ScopeExit curl_close([&curl] {
        curl_easy_cleanup(curl);
    });



    curl_easy_setopt(curl, CURLOPT_URL, ipPort.c_str());
    // set http method to POST
    curl_easy_setopt(curl, CURLOPT_POST, 1L);


    const char *msg = message.c_str();
    PostData post_data;
    post_data.ptr = msg;
    post_data.size = strlen(msg);
    curl_easy_setopt(curl, CURLOPT_READDATA, &post_data);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(msg));

    // set callback on receiving data
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);

    // execute
    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
    {
        std::cout<<"Error on curl_easy_perform: "<< curl_easy_strerror(res)<<std::endl;
    }
}

int main()
{
    std::string ipPort;
    std::string message;

    std::cout<<"Insert destination ip with port: ";
    std::cin>>ipPort;
    std::cout<<std::endl;


    std::cout<<"Insert messagge: ";
    std::cin>>message;
    std::cout<<std::endl;

    std::cout<<"Press p for doing a post request"<<std::endl;


    post_request("http://192.168.1.23:55231","hello");


    return 0;
}

Thank you!谢谢!

I solved the error.我解决了错误。 I wasn't loading the needed dll.我没有加载所需的 dll。

I added libcurl, libcrypto and libssl dlls to the debug/release folder and it worked, with this .pro file:我在 debug/release 文件夹中添加了 libcurl、libcrypto 和 libssl dll,它使用这个 .pro 文件工作:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.cpp


INCLUDEPATH += path/to/curl/include

LIBS += -Lpath/to/curl/lib/folder -lcurl

Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM