Using a proxy for certain URL calls made by app
-
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.
-
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.
@JrDeskJockey said in Using a proxy for certain URL calls made by app:
QNetworkProxyFactory
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.
-
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?
-
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?
-
@JrDeskJockey said in Using a proxy for certain URL calls made by app:
QNetworkProxyFactory
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.
@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!
-
@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!
-
Hi,
@AtoZ said in Using a proxy for certain URL calls made by app:
qt-master
Where does that come from ?
-
Hi,
@AtoZ said in Using a proxy for certain URL calls made by app:
qt-master
Where does that come from ?
-
@AtoZ Try to comment out
load(qttest_p4)
in pro file.
Maybe you need to addCONFIG+=testcase
to the pro file instead.
-
@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)
-
@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.
-
@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?
-
@jsulm @Pablo-J-Rogina Yes, you are right. I think QT += network testlib needs to be added in the .pro file. Since I realized there may be some missing pieces, I created a new unit test project in Qt Creator and migrated the tst_qnetworkproxyfactory code over, and then I was able to run it through Q Creator. Thanks for all your help. My next endeavor will be actually using qnetworkproxyfactory to direct networkflow to use proxy server for external http/s calls and not to use it for internal http/s calls. The project I am working on is a Q Quick project, which mostly built upon qml and javascript, so I am wondering where I should add the networkproxyfactory piece. Shall I create a subclass of qnetworkproxyfactory and use it in main.cpp (this is the way I am trying) or actually I have to modify and recompile network module (which I am trying to avoid) - because I am unsure of how to use qnetworkproxyfactory to track the http/https calls which are actually set in qml. Do you have any suggestions? Thanks again.
-
@jsulm @Pablo-J-Rogina Yes, you are right. I think QT += network testlib needs to be added in the .pro file. Since I realized there may be some missing pieces, I created a new unit test project in Qt Creator and migrated the tst_qnetworkproxyfactory code over, and then I was able to run it through Q Creator. Thanks for all your help. My next endeavor will be actually using qnetworkproxyfactory to direct networkflow to use proxy server for external http/s calls and not to use it for internal http/s calls. The project I am working on is a Q Quick project, which mostly built upon qml and javascript, so I am wondering where I should add the networkproxyfactory piece. Shall I create a subclass of qnetworkproxyfactory and use it in main.cpp (this is the way I am trying) or actually I have to modify and recompile network module (which I am trying to avoid) - because I am unsure of how to use qnetworkproxyfactory to track the http/https calls which are actually set in qml. Do you have any suggestions? Thanks again.
@AtoZ said in Using a proxy for certain URL calls made by app:
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)?
-
@AtoZ said in Using a proxy for certain URL calls made by app:
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.