libcurl http get

#include <dlfcn.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <iconv.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <errno.h>

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


size_t WriteData(void *buffer, size_t size, size_t nmemb, FILE *file)
{
    size_t r_size = fwrite(buffer, size, nmemb, file);

    std::cout << "===============" << (char*)buffer << std::endl;
    fclose(file);
    return r_size;
}

int main()
{

    #define GETURL "http://10.0.1.77:8999/blacklist?id=132623198111196400"

    char path[] = "save_file.txt";
    FILE *file = fopen(path,"w");

    CURL * m_curl;
    m_curl = ::curl_easy_init();

    curl_slist *plist = curl_slist_append(NULL, "Connection: keep-alive");
    plist = curl_slist_append(plist,
                    "Content-Type:application/json");
    ::curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, plist);
    ::curl_easy_setopt( m_curl, CURLOPT_URL, GETURL );

    ::curl_easy_setopt( m_curl, CURLOPT_TIMEOUT, 2);
    ::curl_easy_setopt( m_curl, CURLOPT_CONNECTTIMEOUT, 2);
    ::curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, WriteData);
    ::curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, file);
    CURLcode res = ::curl_easy_perform( m_curl);

    curl_slist_free_all(plist);

    ::curl_global_cleanup();

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的使用libcurl实现HTTP服务器的C代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #define PORT 8080 static size_t handle_request(void *ptr, size_t size, size_t nmemb, void *userdata) { CURL *curl = (CURL *)userdata; char *request = (char *)malloc(size * nmemb + 1); memcpy(request, ptr, size * nmemb); request[size * nmemb] = '\0'; printf("Received request:\n%s\n", request); if (strstr(request, "GET /") == request) { const char *response = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n<html><body><h1>Hello, World!</h1></body></html>"; curl_easy_setopt(curl, CURLOPT_POSTFIELDS, response); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(response)); } else { const char *response = "HTTP/1.1 404 Not Found\nContent-Type: text/html\n\n<html><body><h1>404 Not Found</h1></body></html>"; curl_easy_setopt(curl, CURLOPT_POSTFIELDS, response); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(response)); } free(request); return size * nmemb; } int main() { CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle_request); curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl); CURLcode res; struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: text/html"); if (curl) { curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_PORT, PORT); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } curl_slist_free_all(headers); } return 0; } ``` 这个例子实现了一个简单的HTTP服务器,监听8080端口,并在收到GET请求时返回一个Hello World的HTML页面。如果收到其他类型的请求,服务器将返回404 Not Found。 在这个例子中,我们使用了libcurl提供的CURLOPT_WRITEFUNCTION选项来设置一个回调函数来处理接收到的请求。我们使用了curl_easy_setopt函数来设置libcurl的参数,并使用curl_easy_perform函数来启动HTTP服务器。我们还使用了curl_slist_append函数来添加一个Content-Type头部。 该程序可以使用以下命令编译: ``` gcc -o http_server http_server.c -lcurl ``` 然后可以使用以下命令来运行HTTP服务器: ``` ./http_server ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值