Webpage returning HTTP 406 error only when connecting from Qt
Asked Answered
C

2

6

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.

Closemouthed answered 19/1, 2013 at 17:17 Comment(0)
T
9

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...

Trend answered 20/2, 2013 at 8:44 Comment(1)
Had the same issue. Solved by adding the header. Thanks !Tamikatamiko
A
1

The following solution makes a deal for me:

    request.setRawHeader( "Accept" , "*/*" );
Amboceptor answered 5/10, 2015 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.