簡體   English   中英

跨平台,具有異步能力的 C/C++ HTTP 庫

[英]Cross platform , C/C++ HTTP library with asynchronous capability

我正在尋找一個可以在 Windows 和 Linux 上運行的 C/C++ 庫,這將允許我異步查詢多個網絡服務器(每分鍾 1000 個)以獲取頁眉並以與 WinHttp 庫在 Windows 環境中所做的幾乎相同的方式下載網頁.

到目前為止,我遇到了 libCurl,它似乎可以做我想做的事,但異步方面看起來很可疑。

你認為繞過使用庫的想法並基於可以實現這一點的套接字從頭開始編寫一些簡單的東西有多容易?

任何意見,建議或建議將非常受歡迎。

附錄:- 任何機構對使用 libCurl 進行此操作有任何意見,我說異步方面可能看起來很可疑,但有人對此有任何經驗嗎?

嘗試libevent HTTP 例程。 您創建一個 HTTP 連接並提供一個回調,該回調在響應到達(或超時事件觸發)時調用。

更新:我構建了一個分布式 HTTP 連接限制代理,並在同一個守護進程中使用了客戶端和服務器部分,所有這些都在一個線程上。 它工作得很好。

如果您正在編寫 HTTP 客戶端,libevent 應該是一個不錯的選擇。 我在服務器端遇到的唯一限制是缺少配置選項——如果你想開始添加更高級的功能,API 有點稀疏; 這是我所期望的,因為它從未打算取代 Apache、Nginx 等通用 Web 服務器。 例如,我修補它以添加一個自定義子例程來限制入站 HTTP 請求的總體大小(例如,讀取 10MB 后關閉連接)。 代碼寫得很好,補丁很容易實現。

我使用的是 1.3.x 分支; 2.x 分支比舊版本有一些嚴重的性能改進

代碼示例:找到幾分鍾並編寫了一個快速示例。 這應該讓您熟悉 libevent 編程風格:

#include <stdio.h>
#include <event.h>
#include <evhttp.h>

void
_reqhandler(struct evhttp_request *req, void *state)
{
    printf("in _reqhandler. state == %s\n", (char *) state);
    if (req == NULL) {
        printf("timed out!\n");
    } else if (req->response_code == 0) {
        printf("connection refused!\n");
    } else if (req->response_code != 200) {
        printf("error: %u %s\n", req->response_code, req->response_code_line);
    } else {
        printf("success: %u %s\n", req->response_code, req->response_code_line);
    }
    event_loopexit(NULL);
}

int
main(int argc, char *argv[])
{
    const char *state = "misc. state you can pass as argument to your handler";
    const char *addr = "127.0.0.1";
    unsigned int port = 80;
    struct evhttp_connection *conn;
    struct evhttp_request *req;

    printf("initializing libevent subsystem..\n");
    event_init();

    conn = evhttp_connection_new(addr, port);
    evhttp_connection_set_timeout(conn, 5);
    req = evhttp_request_new(_reqhandler, (void *)state);
    evhttp_add_header(req->output_headers, "Host", addr);
    evhttp_add_header(req->output_headers, "Content-Length", "0");
    evhttp_make_request(conn, req, EVHTTP_REQ_GET, "/");

    printf("starting event loop..\n");
    event_dispatch();

    return 0;
}

編譯並運行:

% gcc -o foo foo.c -levent
% ./foo    
initializing libevent subsystem..
starting event loop..
in _reqhandler. state == misc. state you can pass as argument to your handler
success: 200 OK

Microsoft 的cpprestsdk是一個跨平台的 http 庫,可以與 http 服務器進行通信。 是 msdn 上的一些示例代碼。 這在 linux 上使用 boost asio,在 windows 上使用 WinHttp

試試https://github.com/ithewei/libhv

libhv 是一個跨平台的輕量級網絡庫,用於開發 TCP/UDP/SSL/HTTP/WebSocket 客戶端/服務器。

HTTP客戶端示例:

#include "requests.h"

int main() {
    auto resp = requests::get("http://www.example.com");
    if (resp == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp->status_code, resp->status_message());
        printf("%s\n", resp->body.c_str());
    }

    resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
    if (resp == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp->status_code, resp->status_message());
        printf("%s\n", resp->body.c_str());
    }

    return 0;
}

更多用法見https://github.com/ithewei/libhv/blob/master/examples/http_client_test.cpp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM