如何从URL中提取参数,无论其编写方式如何? [英] How to extract parameters from a URL regardless the way it written by?

查看:155
本文介绍了如何从URL中提取参数,无论其编写方式如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种java方法来提取URL的参数,无论这些参数是如何写入的,都是常规方式,如( https://www.facebook.com/Doly.mohamed.Smile9?ref=stream&hc_location=stream )它很容易因为我所有的必须做的是:

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中提取参数,无论其编写方式如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆