如何使用QNetworkAccessManager和QNetworkReply正确执行http GET请求? URL如何影响Qt中的请求? [英] How to properly do a http GET request using QNetworkAccessManager and QNetworkReply? How does the URL affect the request in Qt?

查看:612
本文介绍了如何使用QNetworkAccessManager和QNetworkReply正确执行http GET请求? URL如何影响Qt中的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对这个问题的跟进。起初我认为在查看了来自Qt的示例后问题得到解决wiki (我使用相同的代码而没有一次更改)。然而,它似乎是URL的罪魁祸首。我尝试使用答案中提供的链接来测试我的http GET请求。使用Http Requester(用于Http请求的Firefox插件(GET,POST等))和 curl 显示此链接没有问题^:

This is a follow up of this question. At first I thought the issue was resolved after checking out the example from the Qt wiki (I use the same code without a single change). However it appears that it's the URL that is the culprit. I tried using the links provided in this answer to test my http GET request. Using the Http Requester (Firefox addon for Http requests (GET, POST etc.)) and curl shows no issues with this link^:

$~: curl --request GET --url "http://httpbin.org/ip" 

由于某种原因,Qt卡住了, readyRead() / 已完成() 永远不会发出信号。

For some reason Qt gets stuck and the readyRead()/finished() signals are never emitted.

因此,由于套接字超时,请求会在一段时间后被取消...对于非常小的东西,并且在不到一秒的时间内由Firefox打开。

As a result the request gets cancelled after some time due to socket timeout...For something that is really small and opened by Firefox in less than a second.

在谈到Http时,我不是专家。我想知道为什么在Qt中出现这种行为,而在使用其他工具时没有任何迹象。

I'm far from an expert when it comes to Http stuff. I'd like to know why this behaviour occurs in Qt while there is no sign of it when working with other tools.

编辑:我还使用Python测试了有问题的URL及其 urllib

I've also tested the problematic URLs using Python and its urllib

import urllib.request
res = urllib.request.urlopen("http://httpbin.org/ip").read().decode("utf-8") 

import xml.etree.ElementTree as ET
doc = ET.fromstring(res)

它的工作原理正好。很明显,Qt正在发生和/或我在使用它时遗漏了一些东西。

and it works just fine. Clearly there is something with Qt going on and/or me missing something when using it.

EDIT2:我还尝试了另一项测试服务对于HTTP请求 - https://postman-echo.com 。使用 curl 没有问题:

I've also tried another test service for HTTP requests - https://postman-echo.com. With curl there is no problem:

$~: curl --request GET --url "https://postman-echo.com/get?foo1=bar1&foo2=bar2"

令我惊讶的是,Qt也没有问题!我在这里看到的唯一不同的是, postman-echo.com 使用HTTPS,而我尝试的其他网址是HTTP。我排除了 https://www.qt.io 这是Qt示例中的默认网址并且工作正常(尽管它没有任何参数)。

For my surprise though there is no problem with Qt either! The only thing that I see here as a huge difference is that postman-echo.com uses HTTPS while the other URLs I've tried were HTTP. I exclude the https://www.qt.io which was the default URL in the Qt example and worked just fine (though it didn't have any parameters).

推荐答案

我忘了提到我落后了。坦率地说,我因为错过了这个而感到相当愚蠢,也没有在工作中检查访客网络(这绕过了愚蠢的代理)。我的一位同事尝试使用HTTP S 而不是HTTP(这是原始链接)。 HTTPS也是代理只允许通过而没有任何问题的东西。它起作用了。

I forgot to mention that I'm behind. Frankly I feel rather stupid for missing this and also not checking through the guest network at work (which circumvents the stupid proxy). A colleague of mine tried using HTTPS instead of HTTP (which is the original link). The HTTPS is also something that the proxy just lets go through without any issues. And it worked.

然而,一个更中立的解决方案是(正如我的同事发现的那样)使用 QNetworkProxyFactory :: setUseSystemConfiguration(true)采用我在系统范围内的代理配置。

However a more neutral solution is (as my colleagues found out) to use QNetworkProxyFactory::setUseSystemConfiguration(true) which takes the proxy configuration that I have system-wide.

这篇关于如何使用QNetworkAccessManager和QNetworkReply正确执行http GET请求? URL如何影响Qt中的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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