I have an url and would like to parse and extract params from it. My implementation is based on the following stackoverflow post
However my url is more complex than the one used in the post above. It looks like this:
https://example.com/cdscontent/login?initialURI=https%3A%2F%2Fexample.com%2Fdashboard%2F%3Fportal%3Dmyportal%26LO%3D4%26contentid%3D10007.786471%26viewmode%3Dcontent%26variant%3D%2Fmyportal%2F
As you can see it has the param initialURI
which is (encoded) url itself and the order of the params in it cannot be changed.
When I run org.apache.http.client.utils.URLEncodedUtils#parse
it returns
[initialURI=https://example.com/dashboard/?portal=myportal, LO=4, contentid=10007.786471, viewmode=content, variant=/myportal/]
as you can see it parses every param except portal
. It is still bound to https://example.com/dashboard/
In other words I am expecting this:
[initialURI=https://example.com/dashboard/, portal=myportal, LO=4, contentid=10007.786471, viewmode=content, variant=/myportal/]
Am I doing here something wrong or do you think that URLEncodedUtils#parse cannot handle this case?
Do you have any alternative to suggest?
Thx a lot!
Unit test to try
public class UrlParserTest {
@Test
public void testParseUrl() throws UnsupportedEncodingException, URISyntaxException {
String url =
"https://www.example.com/cdscontent/login?initialURI=https%3A%2F%2Fwww.example.com%2Fdashboard%2F%3Fportal%3Dmyportal%26LO%3D4%26contentid%3D10007.786471%26viewmode%3Dcontent%26variant%3D%2Fmyportal%2F";
String decoded = URLDecoder.decode(url, "UTF-8");
List<NameValuePair> params = URLEncodedUtils.parse(new URI(decoded), "UTF-8");
System.out.println(params);
}
}