简体   繁体   English

如何使用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?

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). 最初,我认为从Qt Wiki检出示例后,问题就解决了(我使用相同的代码,没有做任何更改)。 However it appears that it's the URL that is the culprit. 但是,似乎是URL才是罪魁祸首。 I tried using the links provided in this answer to test my http GET request. 我尝试使用答案中提供的链接来测试我的http GET请求。 Using the Http Requester (Firefox addon for Http requests (GET, POST etc.)) and curl shows no issues with this link^: 使用Http Requester(用于Http请求(GET,POST等)的Firefox插件)和curl显示此链接没有问题^:

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

For some reason Qt gets stuck and the readyRead() / finished() signals are never emitted. 由于某些原因,Qt卡住了,并且从不发出readyRead() / readyRead() finished()信号。

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. 结果,由于套接字超时,请求在一段时间后被取消了。。。对于很小的东西,Firefox在不到一秒钟的时间内就打开了。

I'm far from an expert when it comes to Http stuff. 在Http方面,我远非专家。 I'd like to know why this behaviour occurs in Qt while there is no sign of it when working with other tools. 我想知道为什么在Qt中会出现这种现象,而在使用其他工具时却没有迹象表明。

EDIT: I've also tested the problematic URLs using Python and its urllib 编辑:我还使用Python及其urllib测试了有问题的URL

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)

and it works just fine. 而且效果很好。 Clearly there is something with Qt going on and/or me missing something when using it. 显然,Qt正在进行某些操作,并且/或者我在使用它时丢失了某些内容。

EDIT2: I've also tried another test service for HTTP requests - https://postman-echo.com . EDIT2:我也尝试HTTP请求另一个测试服务- https://postman-echo.com With curl there is no problem: 使用curl没问题:

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

For my surprise though there is no problem with Qt either! 令我惊讶的是,Qt也没有问题! 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. 我在这里看到的唯一巨大差异是postman-echo.com使用HTTPS,而我尝试过的其他URL是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). 我排除了https://www.qt.io ,它是Qt示例中的默认URL,并且可以正常工作(尽管它没有任何参数)。

Try executing that in an event loop. 尝试在事件循环中执行。 Here is something similar to what I do in a non-gui application: 这类似于我在非GUI应用程序中所做的事情:

QUrl req_url = QUrl(href);
QNetworkRequest request(req_url);

//request.setRawHeader("Content-Type", "application/json;utf8");
//q_nam is QNetworkAccessManager created earlier
QNetworkReply *reply = q_nam->get(request);

QEventLoop event_loop;
connect(q_nam, SIGNAL(finished(QNetworkReply * ) ), &event_loop, SLOT(quit() ) );
event_loop.exec(); // blocks stack until "finished()" has been called
event_loop.processEvents(QEventLoop::ExcludeUserInputEvents, 500 );//what events to processed and for how long
event_loop.exit();

QNetworkReply::NetworkError er = reply->error();
// ....continue handling

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 HTTP S instead of HTTP (which is the original link). 我的一位同事尝试使用HTTP S代替HTTP(原始链接)。 The HTTPS is also something that the proxy just lets go through without any issues. HTTPS也是代理可以毫无问题地通过的。 And it worked. 而且有效。

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. 但是(如我的同事所发现的)更中立的解决方案是使用QNetworkProxyFactory::setUseSystemConfiguration(true) ,它采用了我在系统范围内的代理配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM