问题
I have a test page setup at http://mlecturedownload.com/test-qt.php that has the following code:
<?php
$foo = $_GET['foo'];
if ($foo == "0" || $foo == "1") {
echo $foo;
} else {
echo "Invalid foo";
}
Going to this page in a web browser and modifying the value of foo works fine, and using curl works fine as well. But when I send a request from Qt I get an HTTP 406 error code, which means "Not acceptable". Here is how I make the request:
void MainWindow::on_send_clicked()
{
QString url = "http://mlecturedownload.com/test-qt.php?foo=1";
QNetworkRequest request;
request.setUrl(QUrl(url));
nam->get(request); // nam is a QNetworkAccessManager
}
Here's the entire HTTP request and response obtained from Wireshark
GET /test-qt.php?foo=1 HTTP/1.1
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-CA,*
User-Agent: Mozilla/5.0
Host: mlecturedownload.com
HTTP/1.1 406 Not Acceptable
Date: Sat, 19 Jan 2013 17:14:37 GMT
Server: Apache
Content-Length: 275
Keep-Alive: timeout=5, max=75
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
<head><title>Not Acceptable!</title><script src='/google_analytics_auto.js'></script></head><body><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></body></html>
It looks like an Apache module on the server called mod_security is causing the problem. There are ways to disable this through the .htaccess, but none have them have worked for me. But I'd rather fix the problem instead of disabling the module. Does anyone know what's going on?
Edit: The link to the site I posted is hosted on HostGator. I made a test page on my EC2 server and it worked fine, probably because I don't have that security module enabled. I still need to get it working on the HostGator server.
回答1:
I'm having the same problem - getting the "not acceptable" error when trying to download a file. I did some googling and experimenting and I just found that adding:
request.setRawHeader( "User-Agent" , "Mozilla Firefox" );
before get(request) changes the outcome. :)
I guess the user agent of QNetworkRequest is not recognizable by the server and that's causing the 406 error message. I need to do more tests, but I thought I should share my breakthrough. I hope it helps...
回答2:
The following solution makes a deal for me:
request.setRawHeader( "Accept" , "*/*" );
回答3:
It's more like a blocked User agent. Set User Agent header to anything else and it should work fine.
request.setHeader(QNetworkRequest::UserAgentHeader, "Whatever you like");
http://amin-ahmadi.com/2016/06/13/fix-modsecurity-issues-in-qt-network-module-download-functionality/
来源:https://stackoverflow.com/questions/14416786/webpage-returning-http-406-error-only-when-connecting-from-qt