Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QNAM and Multiple Proxies
Wallboy last edited by Wallboy
Hello, I have a question how how to implement using multiple proxies with a single QNAM instance.
I have multiple proxies that I want to use for my requests and I want the ability to select which proxy to use based off of application variables such as a setAttribute(User, proxyToUse) of a QNetworkRequest. At first I thought I could do this with subclassing QNetworkProxyFactory and reimplementing queryProxy() and then setting setProxyFactory on the QNAM, however there doesn't seem to be any way to customize which proxy to use within this subclass based off any user specific variables. You only seem to be able to access values specific to the QNetworkProxyQuery object in queryProxy() that QNAM passes in. URL, Scheme, Host, etc, but no way to access the QNetworkRequest that this particular request belongs to as far as I can tell.
So it seems the only way to do this is using setProxy() before every single request. I would have thought QNetworkRequest would have a way to set the proxy for this particular request, but alas I don't see anything like that in the docs.
Let me know if you guys have any other ideas, otherwise I'll just go the route of using setProxy() for every request.
I may be wrong but wouldn't QNetworkProxyFactory be what you are looking for ?
Wallboy last edited by
It does seem the most logical class to use for this. I originally subclassed it and reimplemented queryProxy(), but then could not figure out how I was supposed to use user specific variables for it when the only object passed to it is a QNetworkProxyQuery object.
If I could figure out what what calls queryProxy() within QNAM, I could maybe reimplement that and then pass along additional user parameters to it.
What kind of user parameters do you have in mind ?
Wallboy last edited by Wallboy
Class specific variables. I may need to make a request to the exact same URL, but with a different proxy and cookie jar. And as far as I can tell if I subclass QNetworkProxyFactory and reimplement queryProxy(), the QNetworkProxyQuery object that QNAM passes to it, does not contain any sort of function I can use to know which proxy I need to use for that particular request since the URL, port, etc is identical.
Basically I need a way to tell which proxy to use where I create my QNetworkRequest. It would have been nice if QNetworkRequest had a setAttribute(ProxyToUse, proxy). But I guess that would be the same thing as just setProxy(), which at this point is what I'm going to do just before calling get(), post(), etc. It just seems this function was designed to be used for setting a global proxy to use since it's a QNAM function, instead of being called for every single request. But as long as it works, I guess it doesn't matter.
QNetworkProxyFactory only seems useful if you want to select a proxy based off the functions in QNetworkProxyQuery like peerPort() or url().
The factory design (from what I gathered) will indeed use the query specifics in order to select the matching proxy. Your use case is unusual since you want to use the exact same request with different proxies. Thus setting it yourself before sending the request looks like the best solution.