All Products
Search
Document Center

HTTPDNS:Integrate the HTTPDNS C SDK into Qt applications

Last Updated:Mar 25, 2026

You have already learned the full workflow for integrating, initializing, and performing domain name resolution with the C SDK. This topic explains how to apply HTTPDNS C SDK resolution results to network requests made by QNetworkAccessManager in Qt applications.

1. Background

QNetworkAccessManager is Qt’s core network module for sending HTTP and HTTPS requests. It is the most common way to make network requests in Qt applications. This topic explains how to integrate the HTTPDNS C SDK into Qt applications that use QNetworkAccessManager. If you use Qt WebEngine (WebView scenarios), see Integrating DoH with Qt WebEngine.

2. SDK integration

For complete instructions on SDK initialization, configuration, and domain name resolution, see C SDK. The main example code is as follows:

#include "hdns_api.h"

// Initialize the SDK
hdns_sdk_init();
hdns_client_t *client = hdns_client_create("your_account_id", "your_secret_key");

// Configure the client
hdns_client_set_timeout(client, 2000);
hdns_client_set_using_cache(client, true);
hdns_client_set_using_https(client, true);
hdns_client_set_using_sign(client, true);
hdns_client_enable_expired_ip(client, true);

// Start the client
hdns_client_start(client);

// Resolve a domain name
hdns_list_head_t *results = NULL;
hdns_status_t status = hdns_get_result_for_host_sync_with_cache(
    client, "your.domain.com", HDNS_QUERY_AUTO, NULL, &results);

char ip[HDNS_IP_ADDRESS_STRING_LENGTH];
if (hdns_status_is_ok(&status)) {
    if (hdns_select_ip_randomly(results, HDNS_QUERY_AUTO, ip) == HDNS_OK) {
        // Use the resolved IP to send your business request (see Section 3)
    }
}
hdns_list_cleanup(results);

3. Adapt requests for QNetworkAccessManager

After you retrieve the HTTPDNS resolution result, apply it to QNetworkAccessManager requests. Because you replace the domain name in the URL with an IP address, you must also set the Host header and configure HTTPS certificate validation. This ensures the server and TLS handshake correctly identify the original domain name.

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

QUrl url("https://your.domain.com/api/path");
QString originalHost = url.host();

// Replace the domain name in the URL with the IP resolved by HTTPDNS
url.setHost(resolvedIp);

// Build the request
QNetworkRequest req;
req.setUrl(url);
req.setRawHeader("Host", originalHost.toUtf8());  // Keep the Host header set to the original domain name
req.setPeerVerifyName(originalHost);               // Validate the HTTPS certificate against the original domain name

// Send the request
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(req);

4. Summary

To integrate the HTTPDNS C SDK into Qt applications using QNetworkAccessManager, follow three key steps: replace the domain name in the URL with the resolved IP address, set the Host header, and set the certificate verification domain name. This solution is simple to implement. It correctly handles SNI and certificate validation for HTTPS using setPeerVerifyName. You do not need to skip or ignore certificate validation.