Last active
May 14, 2020 08:16
-
-
Save redochka/50b399da43c2170348f1d7b30a3820a0 to your computer and use it in GitHub Desktop.
Extract request parameter from query string in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.NameValuePair; | |
import org.apache.http.client.utils.URLEncodedUtils; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.nio.charset.StandardCharsets; | |
import java.util.List; | |
public class UrlUtils { | |
public static String extractValueOfParamFromQueryString(String name, String qs) throws Exception{ | |
String queryString = "?" + qs; | |
return UrlUtils.extractValueOfParamFromUrl(name, queryString); | |
} | |
public static String extractValueOfParamFromUrl(String name, String url) throws URISyntaxException { | |
List<NameValuePair> nvps = URLEncodedUtils.parse(new URI(url), StandardCharsets.UTF_8); | |
for (NameValuePair nameValuePair : nvps) { | |
if(name.equals(nameValuePair.getName())) { | |
return nameValuePair.getValue(); | |
} | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment