gpt4 book ai didi

c++ - 向网站发送 http get 请求,忽略 c++ 的响应

转载 作者:可可西里 更新时间:2023-11-01 16:38:55 34 4
gpt4 key购买 nike

我需要使用 C++ 发送一个 http get 请求。我现在的代码是:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main ()
{
ifstream llfile;
llfile.open("C:/AdobeRenderServerLog.txt");

if(!llfile.is_open()){
exit(EXIT_FAILURE);
}

char word[50];
llfile >> word;
cout << word;
llfile.close();
return 0;
}

请求将发送到:

www.example.com/logger.php?data=word

最佳答案

可能最简单的方法是使用 libCurl .

使用“easy interface” ' 你只需调用 curl_easy_init(),然后调用 curl_easy_setopt() 来设置 url,然后调用 curl_easy_perform() 使其完成获取。如果您想要响应(或进度等),请在 setopt() 中设置适当的属性。完成所有操作后,调用 curl_easy_cleanup()。完成任务!

文档非常全面 - 它不仅仅是一个获取 http 请求的简单库,而且几乎涵盖所有网络协议(protocol)。意识到该文档因此看起来非常复杂,但实际上并非如此。

直接转到 example 可能是个好主意代码,简单的看起来像这样:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
res = curl_easy_perform(curl);

/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

但您可能想查看“get a file in memory” '样本或'replace fopen ' 还有一个。

关于c++ - 向网站发送 http get 请求,忽略 c++ 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10433649/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com