仅从Qt连接时,网页返回HTTP 406错误 [英] Webpage returning HTTP 406 error only when connecting from Qt

查看:260
本文介绍了仅从Qt连接时,网页返回HTTP 406错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://mlecturedownload.com/test-qt.php具有以下代码:

<?php

$foo = $_GET['foo'];
if ($foo == "0" || $foo == "1") {
  echo $foo;
} else {
  echo "Invalid foo";
}

在Web浏览器中转到此页面并修改foo的值可以很好地工作,而使用curl也可以很好地工作.但是,当我从Qt发送请求时,收到HTTP 406错误代码,表示不可接受".这是我发出请求的方式:

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
}

这是从Wireshark获得的整个HTTP请求和响应

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>

看起来服务器上的一个名为mod_security的Apache模块引起了该问题.有几种方法可以通过.htaccess禁用此功能,但是没有一种方法对我有用.但是我宁愿解决问题而不是禁用模块.有人知道发生了什么事吗?

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?

我发布的网站的链接托管在HostGator上.我在EC2服务器上做了一个测试页,它工作正常,可能是因为我没有启用该安全模块.我仍然需要在HostGator服务器上运行它.

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.

推荐答案

我遇到了同样的问题-尝试下载文件时出现不可接受"错误.我做了一些谷歌搜索和实验,发现添加了:

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" );

在get(request)更改结果之前. :)

before get(request) changes the outcome. :)

我猜想 QNetworkRequest 的用户代理无法被服务器识别,这导致了406错误消息. 我需要做更多的测试,但我认为我应该分享自己的突破. 希望对您有帮助...

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

这篇关于仅从Qt连接时,网页返回HTTP 406错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆