What Raw Header/Header to Use for Qt File Download with QNetworkRequest

183 Views Asked by At

I need my Qt program to download a file from my website.

A recent web host transfer from BlueHost to Siteground broke the file download code. With Siteground, I get "Connection Closed" for QNetworkReply::errorString(), and QNetworkReply::NetworkError value 2 for QNetworkReply::error() with the old code.

I fixed it by simply removing the setRawHeader("User-Agent","Mozilla Firefox") code, and everything is happy now.

My question is, is it OK to not use a header or raw header for file downloads, as in the code below? How come I needed the setRawHeader() line to make the file download work with the BlueHost website, but not with the Siteground website?

**update might have been Hostgator that required the header not sure

void program::checkForUpdates()
{
    QString versionAddress("https://***.com/file");
    QUrl versionUrl;
    versionUrl = QUrl(versionAddress);
    QNetworkRequest request(versionUrl);
    request.setRawHeader("User-Agent","Mozilla Firefox");
    downloadVersionReply = mNetworkManager->get(request);
    connect(downloadVersionReply, &QNetworkReply::finished, this, &program::downloadFinished);
    connect(downloadVersionReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &program::downloadError);
}

void program::downloadError(QNetworkReply::NetworkError code)
{
    errorString = downloadVersionReply->errorString() + QString::number(downloadVersionReply->error());
    downloadVersionReply->abort();
    downloadVersionReply->deleteLater();
}
1

There are 1 best solutions below

1
Remy Lebeau On

A User-Agent header identifies the type of client that is sending the request. Sometimes, web servers may decide to customize their responses for different UserAgents. For instance, a user-facing web browser may receive a different response than an auto-downloader would receive.

For a simple file download, the User-Agent really shouldn't matter, but apparently it did matter to BlueHost, for whatever reason.

There is really no way to identify up front whether a given web server is UserAgent-sensitive or not. You will just have to test your code and see what happens, or ask the server admins.