Fix ModSecurity Issues in Qt Network Module Download Functionality

You might have come across the following (Not Acceptable) error when trying to download files from some websites using Qt. This error message is generated by ModSecurity and it usually means that for some reason the web server does not allow downloading of the file using the method you are trying. In my case it was because of the User-Agent Header set to a value not allowed by the server. So here is how I fixed it:

First of all I am sharing the error code I received using QNetworkAccessManager::get function.

Not Acceptable!
An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.


Here is the HTML code of the error:

<h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p>

What I did was that I set the User-Agent Header to something else, I actually set it to “Amin” and it worked fine. Here is the piece of code I used for downloading the file:

QString url = "http://amin-ahmadi.com/somefilesomewhere;
QNetworkAccessManager nam;
QNetworkRequest req = QNetworkRequest(QUrl(url));
req.setHeader(QNetworkRequest::UserAgentHeader, "Amin");
QNetworkReply* reply = nam.get(req);
while (!reply->isFinished())
{
	qApp->processEvents();
	continue;
}
QByteArray data = reply->readAll();

// Do whatever you want with data

reply->deleteLater();

Let me know if you face issues using this code.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.