When you use QGIS to Add a WMS Layer through the GUI, when you have selected the service URL and click connect, in the background QGIS runs a WMS GetCapabilities request.
If you don't click the Use Contextual WMS Legend option then the legend that appears is a GetLegendGraphic request like below:
http://ogc.bgs.ac.uk/cgi-bin/BGS_BGS-HPA_Radon_Potential/wms?language=eng&version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=GBR_BGS-HPA_625K_RADON_POTENTIAL&format=image/png&STYLE=default&&TRANSPARENT=true
This URL is parsed from the GetCapabilities response for the selected layer/style. The legend generated has no context, it shows all the classes for the map.
If you do click the Use Contextual WMS Legend option, you still have a GetLegendGraphic request, but this time the request has additional parameters added to the parsed GetLegendGraphic URL to add context.
The contextual GetLegendGraphic request should append CRS/SRS depending on the WMS version, BBOX (in the coordinates of the CRS/SRS), and the width and height (of the GetMap image providing the context), so if your GetMap request is like:
http://ogc.bgs.ac.uk/cgi-bin/BGS_BGS-HPA_Radon_Potential/wms?language=eng&&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX=55.02958881675145619,-4.984571910596027777,57.17968296470549205,-2.783458579470199368&CRS=EPSG:4326&WIDTH=906&HEIGHT=885&LAYERS=GBR_BGS-HPA_625K_RADON_POTENTIAL&STYLES=default&FORMAT=image/png&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE
Your contextual GetLegendGraphic request should be like:
http://ogc.bgs.ac.uk/cgi-bin/BGS_BGS-HPA_Radon_Potential/wms?language=eng&version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=GBR_BGS-HPA_625K_RADON_POTENTIAL&format=image/png&STYLE=default&&TRANSPARENT=true&BBOX=55.02958881675145619,-4.984571910596027777,57.17968296470549205,-2.783458579470199368&CRS=EPSG:4326&WIDTH=906&HEIGHT=885&
The legend returned is contextual, it only shows classes that appear in the associated GetMap request. So as there are no areas > 499.99 Bqm3 in the map, the contextual legend doesn't have this class (red in the non-contextual legend).
In your example the service has an endpoint like:
http://www.geoportaligm.gob.ec/50k/wms?
A GetCapabilities request can be:
http://www.geoportaligm.gob.ec/50k/wms?service=WMS&request=GetCapabilities&
Your example layer igm:acequia_l has two styles (though they look the same to me, so perhaps not the best example)
<Style>
<Name>acequia_l_escala</Name>
<Title>acequia</Title>
<LegendURL width="67" height="40">
<Format>image/png</Format>
<OnlineResource
xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://www.geoportaligm.gob.ec:80/50k/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=igm%3Aacequia_l"/>
</LegendURL>
</Style>
<Style>
<Name>acequia_l</Name>
<Title>acequia</Title>
<LegendURL width="67" height="40">
<Format>image/png</Format>
<OnlineResource
xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://www.geoportaligm.gob.ec:80/50k/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=igm%3Aacequia_l&style=acequia_l"/>
</LegendURL>
</Style>
GetLegendGraphic (non contextual) for style acequia_l_escala for layer igm:acequia_l:
GetLegendGraphic (non contextual) for style acequia_l for layer igm:acequia_l:
At the moment you have urlWithParams = "url=http://www.geoportaligm.gob.ec/50k/wms&format=image/png&layers=igm:acequia_l&styles=&crs=EPSG:32717"
if this is intended to a test URL that generates the legend you will need to change that to be an appropriate contextual GetLegendGraphic request.
In your live code, you will need to work out how to split up the contextual and non-contextual components of the URL, because the contextual component changes every time you move/zoom the map.
In your live code you will probably need to work out which named style to use per selected layer, though you may be able to miss it out and go with the default by using style&