一个基于 QT 编写的开源 HttpClient 库,采用 header-only 的方式实现,只需在你的工程中包含 src 目录的 HttpClient.h 文件即可,可以实现网络请求、文件断点续传下载、身份验证等功能。
简单的使用例子:
HttpClient client; client.get("https://qthub.com") .onSuccess([](QString result) {qDebug()<<"result:"<
接口:
HttpRequest &onSuccess(const QObject *receiver, const char *method); HttpRequest &onSuccess(std::function lambda); HttpRequest &onSuccess(std::function lambda); HttpRequest &onSuccess(std::function lambda);
HttpRequest &onFailed(const QObject *receiver, const char *method); HttpRequest &onFailed(std::function lambda); HttpRequest &onFailed(std::function lambda); HttpRequest &onFailed(std::function lambda);
例子:
static HttpClient client; client.get("https://qthub.com") .onSuccess(this, SLOT(onSuccess(QString))) .onFailed(this, SLOT(onFailed(QString))) .exec(); // 执行 Http 操作
接口:
HttpRequest &onSuccess(std::function lambda); HttpRequest &onSuccess(std::function lambda); HttpRequest &onSuccess(std::function lambda);
HttpRequest &onFailed(std::function lambda); HttpRequest &onFailed(std::function lambda); HttpRequest &onFailed(std::function lambda);
例子:
client.get("https://qthub.com") .onSuccess([](QString result) {qDebug()<<"result:"<
接口:
HttpRequest &onDownloadProgress(const QObject *receiver, const char *method);
例子:
client.get("https://qthub.com") .onSuccess(this, SLOT(onSuccess(QString))) .onDownloadProgress(this, SLOT(onDownloadProgress(qint64, qint64))) .onFailed(this, SLOT(onFailed(QString))) .exec();
接口:
HttpRequest &onDownloadProgress(std::function lambda);
例子:
client.get("https://qthub.com") .onSuccess([](QString result) {qDebug()<<"result:" << result.left(10); }) .onDownloadProgress([](qint64 bytesReceived, qint64 bytesTotal) {qDebug() << "lambda bytes received:" << bytesReceived << "bytes total:" << bytesTotal; }) .onFailed([](QString error) {qDebug()<<"error:" << error; }) .exec();
接口:
HttpRequest &onUploadProgress(const QObject *receiver, const char *method); HttpRequest &onUploadProgress(std::function lambda);
例子:
client.post("http://httpbin.org/post") .bodyWithFile("text_file", "helloworld.txt") .onUploadProgress([](qint64 bytesSent, qint64 bytesTotal) {qDebug() << "lambda bytes sent:" << bytesSent << "bytes total:" << bytesTotal; }) .onSuccess([](QString result) {qDebug()<<"result:" << result.left(100); }) .onFailed([](QString error) {qDebug()<<"error:" << error; }) .exec();
接口:
HttpRequest &timeout(const int &msec = -1);
HttpRequest &onTimeout(const QObject *receiver, const char *method); HttpRequest &onTimeout(std::function lambda); HttpRequest &onTimeout(std::function lambda);
例子:
client.get("https://qthub.com") .onSuccess([](QString result) {qDebug()<<"result:"<
接口:
HttpRequest &block();
例子:
client.get("https://qthub.com") .onSuccess(this, SLOT(onSuccess(QString))) .onFailed(this, SLOT(onFailed(QString))) .block() // 阻塞同步操作 .exec(); // 执行 Http 操作
接口:
HttpRequest &header(const QString &key, const QVariant &value); HttpRequest &headers(const QMap &headers);
例子:
client.post("https://example.com") .header("content-type", "application/json") .queryParam("key", "Hello world!") .body(R"({"user":"test"})") .onSuccess([](QString result){}) .onFailed([](QString error){}) .exec();
接口:
HttpRequest &queryParam(const QString &key, const QVariant &value); HttpRequest &queryParams(const QMap ¶ms);
例子:
client.get("https://example.com") .queryParam("key1", "value1") .queryParam("key2", "value2") .queryParam("key3", "value3") .onSuccess([](QString result){}) .onFailed([](QString error){}) .exec();
上面代码等同于:
client.get("https://example.com?key1=value1&key2=value2&key3=value3") .onSuccess([](QString result){}) .onFailed([](QString error){})
接口:
HttpRequest &body(const QByteArray &raw); HttpRequest &bodyWithRaw(const QByteArray &raw);
HttpRequest &body(const QJsonObject &json); HttpRequest &bodyWithJson(const QJsonObject &json);
HttpRequest &body(const QVariantMap &formUrlencodedMap); HttpRequest &bodyWithFormUrlencoded(const QVariantMap &keyValueMap);
HttpRequest &body(QHttpMultiPart *multiPart); HttpRequest &bodyWithMultiPart(QHttpMultiPart *multiPart);
HttpRequest &body(const QString &key, const QString &file); HttpRequest &bodyWithFile(const QString &key, const QString &file); HttpRequest &bodyWithFile(const QMap &fileMap);
例子:
client.post("http://httpbin.org/post") .body("hello world") .onSuccess([](QString result){qDebug()<
QJsonObject json {{"property1", 1}, {"property2", 2} }; client.post("http://httpbin.org/post") .body(json) .onSuccess([](QString result){qDebug()<
QVariantMap map {{"property1", 1}, {"property2", 2} }; client.post("http://httpbin.org/post") .body(map) .onSuccess([](QString result){qDebug()<
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QFile *file = new QFile("demo.txt"); file->open(QIODevice::ReadOnly); file->setParent(multiPart); QString dispositionHeader = QString("form-data; name=\"%1\";filename=\"%2\"") .arg("text_file") .arg(file->fileName()); QHttpPart part; part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader); part.setBodyDevice(file); multiPart->append(part); QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart->boundary().data()); HttpClient client; client.post("http://httpbin.org/post") .header("content-type", contentType) .body(multiPart) .onSuccess([](QString result){qDebug()<
HttpClient client; client.post("http://httpbin.org/post") .body("text_file", "demo.txt") .body("image_file", "demo.jpg") .onSuccess([](QString result){qDebug()<
接口:
[版权声明] :本文文字、代码及图片版权归原作者所有,任何媒体、网站或个人未经本网协议授权不得采集、整理、转载或以其他方式复制发表。已经本站协议授权的媒体、网站,在使用时必须注明“稿件来源:学研谷”。