4

So as of Qt 5, QHttp is deprecated and we're all supposed to use QNetworkAccessManager now. Fine. But how do I make a request (HTTP or otherwise) from my multihomed machine without feeling like I'm playing roulette?

If there is no way, then what is a workaround? For my specific case right now, I just need to download a file via HTTP. But is there really no way to do this in a generic way with QtNetwork?

László Papp
  • 51,870
  • 39
  • 111
  • 135
njahnke
  • 1,369
  • 2
  • 10
  • 33

2 Answers2

1

The quick workaround would be to use this in your project file

QT += http

It is still available in a separate module for compatibility.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • It is true. But, from the docs ... "We strongly advise against using it in new code." So, I was hoping for a solution people are already using for new code. – njahnke Apr 04 '14 at 23:52
  • @njahnke: to me, it does not matter what they advise if they do not provide alternatives. I have been a happy user of qthttp thus far without worries, but yes, it is just a quick workaround as written. – László Papp Apr 05 '14 at 04:34
0

If you are lucky enough that the desired interface is a separate physical (hardware) interface, you can do e.g. (web_view is a QWebView*):

QNetworkConfigurationManager config_manager;
QList<QNetworkConfiguration> configs = config_manager.allConfigurations();
bool found_interface = false;
QString desired_interface_name("eth1");
foreach (const QNetworkConfiguration &config, configs) {
    if (config.name() == desired_interface_name) {
        found_interface = true;
        QNetworkAccessManager *network_access_manager = new QNetworkAccessManager;
        network_access_manager->setConfiguration(config);
        web_view->page()->setNetworkAccessManager(network_access_manager);
        break;
    }
}
if (!found_interface) {
    //we failed to find the interface!
}

Again, this will not work if the IP is bound to a virtual interface part of one physical interface (e.g. eth1:1, eth1:2, etc). I am still looking for a solution for that case.

njahnke
  • 1,369
  • 2
  • 10
  • 33
  • @njajnke do you manage to succesfully bind the QNetworkAccessManager? I tested some times ago a similar code, but it was not working for me on a multi-homed system (i.e. eth0, eth1) Requests were always routed via default gw – sergico Jun 08 '15 at 10:18