Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Using a proxy for certain URL calls made by app

Using a proxy for certain URL calls made by app

Scheduled Pinned Locked Moved Unsolved C++ Gurus
15 Posts 5 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JrDeskJockey
    wrote on last edited by
    #1

    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......

    1. all requests for "myinternalserver.mycompany.com" do not use a proxy at all
    2. 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.

    Pablo J. RoginaP 1 Reply Last reply
    0
    • J JrDeskJockey

      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......

      1. all requests for "myinternalserver.mycompany.com" do not use a proxy at all
      2. 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.

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @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.

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      A 1 Reply Last reply
      3
      • J Offline
        J Offline
        JrDeskJockey
        wrote on last edited by
        #3

        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?

        jsulmJ 1 Reply Last reply
        0
        • J JrDeskJockey

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JrDeskJockey http://doc.qt.io/qt-5/qnetworkproxyfactory.html#setApplicationProxyFactory

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • Pablo J. RoginaP Pablo J. Rogina

            @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.

            A Offline
            A Offline
            AtoZ
            wrote on last edited by
            #5

            @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!

            jsulmJ 1 Reply Last reply
            0
            • A AtoZ

              @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!

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @AtoZ Try to comment out

              load(qttest_p4)
              

              in pro file.
              Maybe you need to add

              CONFIG+=testcase
              

              to the pro file instead.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                @AtoZ said in Using a proxy for certain URL calls made by app:

                qt-master

                Where does that come from ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  @AtoZ said in Using a proxy for certain URL calls made by app:

                  qt-master

                  Where does that come from ?

                  A Offline
                  A Offline
                  AtoZ
                  wrote on last edited by
                  #8

                  @SGaist qt-master comes from the test example that Pablo referred to in his post.

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @AtoZ Try to comment out

                    load(qttest_p4)
                    

                    in pro file.
                    Maybe you need to add

                    CONFIG+=testcase
                    

                    to the pro file instead.

                    A Offline
                    A Offline
                    AtoZ
                    wrote on last edited by
                    #9

                    @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? 0_1533050237843_unresolved_external_qt.JPG

                    Pablo J. RoginaP jsulmJ 2 Replies Last reply
                    0
                    • A AtoZ

                      @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? 0_1533050237843_unresolved_external_qt.JPG

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @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)

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      A 1 Reply Last reply
                      0
                      • Pablo J. RoginaP Pablo J. Rogina

                        @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)

                        A Offline
                        A Offline
                        AtoZ
                        wrote on last edited by
                        #11

                        @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.

                        1 Reply Last reply
                        0
                        • A AtoZ

                          @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? 0_1533050237843_unresolved_external_qt.JPG

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @AtoZ Try

                          QT += testlib
                          

                          http://doc.qt.io/qt-5/qtest.html

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          A 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @AtoZ Try

                            QT += testlib
                            

                            http://doc.qt.io/qt-5/qtest.html

                            A Offline
                            A Offline
                            AtoZ
                            wrote on last edited by
                            #13

                            @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.

                            Pablo J. RoginaP 1 Reply Last reply
                            0
                            • A AtoZ

                              @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.

                              Pablo J. RoginaP Offline
                              Pablo J. RoginaP Offline
                              Pablo J. Rogina
                              wrote on last edited by
                              #14

                              @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)?

                              Upvote the answer(s) that helped you solve the issue
                              Use "Topic Tools" button to mark your post as Solved
                              Add screenshots via postimage.org
                              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                              A 1 Reply Last reply
                              0
                              • Pablo J. RoginaP Pablo J. Rogina

                                @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)?

                                A Offline
                                A Offline
                                AtoZ
                                wrote on last edited by
                                #15

                                @Pablo-J.-Rogina Sure will do.

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved