QNAM and Multiple Proxies
-
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.
EDIT: Also one more question. For each proxy I also need to use cookies specific to that proxy. What would be the best way to go about doing this? I was thinking making a cookie jar for each proxy and then before each request calling setCookieJar() on the QNAM instance. However, let's say I do this and the request begins downloading a file, and then another request for a different proxy/cookie jar gets called. If the first request had say FollowRedirects set to True, would it still be using the FIRST cookiejar, or would it now be using the one that got set in the second request? I get a bit confused on the asynchronous nature of QNAM and it's state.
Thanks.
-
Hi,
I may be wrong but wouldn't QNetworkProxyFactory be what you are looking for ?
-
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 ?
-
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.