问题描述
我想要一种java方法来提取URL的参数,无论这些参数是如何写入的,都是常规方式,如()它很容易因为我所有的必须做的是:
I want a java way to extract the parameters of a URL regardless the way these parameters are written in it, in the regular way like( https://www.facebook.com/Doly.mohamed.Smile9?ref=stream&hc_location=stream ) it's so easy because all i have to do is :
URL url = new URL("www.blabla....etc");
String query = url.getQuery();
try{
String [] params = query.split("&");
for(int i= 0 ; i < params.length; i++){
String [] split = params[i].split("=");
parameters.put(split[0], split[1]);
}
}catch(NullPointerException ex){}
所以参数值将是:
key = ref value = stream , key = hc_location value = stream
但如果URL的参数以其他方式写入,或者如果URL没有像在案例中那样写入参数,我该怎么办? doPost()
方式。
but what shall i do if the URL has parameters written in another way or if the URL does't has it's parameters written in it like in the case of the doPost()
way.
并且有一种方法可以从URL获取extraPathInfo而无需使用servlets?
and is there is a way to get the extraPathInfo from a URL without using servlets?
推荐答案
您可以使用Apache的HTTP工具轻松完成。
You could do that easily with Apache's HTTP utils.
URIBuilder uriBuilder = new URIBuilder(uriString);
List<NameValuePair> urlParameters = uriBuilder.getQueryParams();
String uriWithoutParameters = uriBuilder.clearParameters().toString();
现在,您可以使用其他类轻松地将GET请求转换为POST请求http utils API。
Now you could, for example, easily convert the GET request to a POST request, using other classes from the http utils API.
这篇关于如何从URL中提取参数,无论其编写方式如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!