4

I use QGIS 2.18.4 with an Osgeo4W install, W7 x64.

The code below works fine without a proxy :

import urllib
params = {
    'service': 'WFS',
    'version': '2.0.0',
    'request': 'GetFeature',
    'typename': 'regbio',
    'srsname': 'EPSG:2154'
}
uri = 'http://ws.carmencarto.fr/WFS/119/fxx_ref?' + urllib.unquote(urllib.urlencode(params))
vlayer = QgsVectorLayer(uri, 'ma couche wfs', 'WFS')
if not vlayer.isValid():
    print u"The layer %s is not valid" %vlayer.name()
idx = vlayer.dataProvider().fieldNameIndex('NOM_DOMAIN')
uv = vlayer.dataProvider().uniqueValues(idx)
uv.sort()
for i, item in enumerate(uv):
    print i, item

The result is :

0 alpin
1 atlantique
2 continental
3 mediterraneen

The problem I'm having is that I cannot get the same result when a proxy is used.

With a proxy, I use the code of this : https://stackoverflow.com/a/37757921 However, the retrieved data are the html code.

Now, I wish to have the same result as the first code, but through a proxy.

2

From this post : https://gis.stackexchange.com/a/214915/93097, just add the following code for, according to the QgsNetworkAccessManager class instructions, setup the NetworkAccessManager according to the user's settings :

network_manager=QgsNetworkAccessManager.instance()
network_manager.setupDefaultProxyAndCache()

Working code :

import urllib
network_manager=QgsNetworkAccessManager.instance()
network_manager.setupDefaultProxyAndCache()
params = {
    'service': 'WFS',
    'version': '2.0.0',
    'request': 'GetFeature',
    'typename': 'regbio',
    'srsname': 'EPSG:2154'
}
uri = 'http://ws.carmencarto.fr/WFS/119/fxx_ref?' + urllib.unquote(urllib.urlencode(params))
vlayer = QgsVectorLayer(uri, 'ma couche wfs', 'WFS')
if not vlayer.isValid():
    print u"The layer %s is not valid" %vlayer.name()
idx = vlayer.dataProvider().fieldNameIndex('NOM_DOMAIN')
uv = vlayer.dataProvider().uniqueValues(idx)
uv.sort()
for i, item in enumerate(uv):
    print i, item
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.