Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Using a proxy for certain URL calls made by app
JrDeskJockey last edited by
Hello, I am fairly new to Qt. I am writing an app in QML but need to set a proxy in main.cpp. I have successfully defined an app-wide proxy with a simple line like...
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::HttpProxy, "one.proxy.myserver.com", 8888));
The issue is that my company requires me to not hit one.proxy.myserver.com when the http request is on our internal network. I have been Googling and reviewing Qt documentation but I'm still baffled. I see there is QNetworkProxyFactory, QNetworkProxyQuery, etc, but I can't seem to figure out how to pull it together. What I'd like to do is say, app wide......
- all requests for "myinternalserver.mycompany.com" do not use a proxy at all
- all other requests hit one.proxy.myserver.com
Is that possible? Does anyone have a small bit of sample code to do that?
Thanks so much for your help.
Not that I'm used that class that much, but it seems to be what you're looking for, in particular method proxyForQuery()
Maybe this test example can also guide you understanding what you need to do.
JrDeskJockey last edited by
So....I don't get it. I wonder, how do you setup the proxy factory? I see the proxy query, you can define a URL and pass that to the factory, but how do you establish the factory in the first place so that it knows of your specific domains you want to track and either exclude or include for a proxy?
@Pablo-J.-Rogina When I downloaded qt-master/tests/auto/qnetworkproxyfactory/qnetworkproxyfactory.pro, and tried to look at it in Qt Creator, I got an error: Cannot find feature qttest_p4. What am I missing? What is qttest_p4 for? Thanks!
@AtoZ Try to comment out
in pro file.
Maybe you need to add
to the pro file instead.
Where does that come from ?
@SGaist qt-master comes from the test example that Pablo referred to in his post.
@jsulm Thanks for your reply. After I comment out load(qttest_p4) and build again, I get unsolved externals issue. Adding CONFIG+=testcase does not solve the issue. I still miss some pieces. Can you please help again?
@AtoZ my suggestion was to look at the test code and figure out how the QNetworkProxyFactory is being used, not to run the test itself. I'm not sure but you may need to start one or two levels up for actually running the test(s)
@Pablo-J.-Rogina Thanks for your tips and quick reply, Pablo. I'd like to see how test is done because it is hard to track network traffic, isn't it? The code you referred does give a good example on how to test a running networkproxyfactory, even if it does not build successfully because of missing libraries, I think.
migrated the tst_qnetworkproxyfactory code over, and then I was able to run it through Q Creator
Glad you have advanced so far.
how to use qnetworkproxyfactory to track the http/https calls which are actually set in qml
May I suggest you open another post on its own since that question is slightly more specific (QML + proxies)?
@Pablo-J.-Rogina Sure will do.