java 如何从 HTTP“Referer”标头获取参数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1124614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the parameter values from HTTP "Referer" header?
提问by Thomas_
I got the url value using request.getHeader("Referer")e.g.:
我使用request.getHeader("Referer")例如获取 url 值:
string rr=request.getHeader("Referer");
<%= rr %>
I got the url as http://www.sun.com/questions?uid=21&value=gg
我得到了网址 http://www.sun.com/questions?uid=21&value=gg
Now I stored that url in as string, how do I get the value parameter value as uid=21and value=gg
现在我将该 url 存储为字符串,如何将 value 参数值作为uid=21和value=gg
回答by Brian Agnew
You need to:
你需要:
- take the string after the '?'
- split that on '&' (you can do this and the above by using the URL object and calling getQuery())
- You'll then have strings of the form 'x=y'. Split on the first '='
- URLDecodethe result parameter values.
- 在 '?' 后面取字符串
- 将其拆分为“&”(您可以通过使用 URL 对象并调用 getQuery() 来执行此操作和以上操作)
- 然后,您将拥有“x=y”形式的字符串。在第一个 '=' 处拆分
- URLDecode结果参数值。
This is all a bit messy, unfortunately.
不幸的是,这一切都有些混乱。
Why the URLDecode step ? Because the URL will be encoded such that '=' and '?' in parameter values won't confuse a parser.
为什么是 URLDecode 步骤?因为 URL 将被编码为 '=' 和 '?' in 参数值不会混淆解析器。
回答by Brian Agnew
This tutorialmight help a bit.
本教程可能会有所帮助。
What you need to do is parse the URL, then get the 'query' property, then parse it into name/value pairs.
您需要做的是解析 URL,然后获取 'query' 属性,然后将其解析为名称/值对。
So something like this:
所以像这样:
URL url = new URL(referer);
String queryStr = url.getQuery();
String[] params = queryStr.split("&");
for (String param: params) {
String key = param.substring(0, param.indexOf('=');
String val = param.substring(param.indexOf('=') + 1);
}
Disclaimer: this has not been tested, and you will need to do more error checking!
免责声明:这还没有经过测试,你需要做更多的错误检查!
回答by Joe23
Below an example how Apache Solr does it (SolrRequestParsers).
下面是 Apache Solr 如何做到这一点的示例 (SolrRequestParsers)。
/**
* Given a standard query string map it into solr params
*/
public static MultiMapSolrParams parseQueryString(String queryString)
{
Map<String,String[]> map = new HashMap<String, String[]>();
if( queryString != null && queryString.length() > 0 ) {
try {
for( String kv : queryString.split( "&" ) ) {
int idx = kv.indexOf( '=' );
if( idx > 0 ) {
String name = URLDecoder.decode( kv.substring( 0, idx ), "UTF-8");
String value = URLDecoder.decode( kv.substring( idx+1 ), "UTF-8");
MultiMapSolrParams.addParam( name, value, map );
}
else {
String name = URLDecoder.decode( kv, "UTF-8" );
MultiMapSolrParams.addParam( name, "", map );
}
}
}
catch( UnsupportedEncodingException uex ) {
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, uex );
}
}
return new MultiMapSolrParams( map );
}
Usage:
用法:
MultiMapSolrParams params = SolrRequestParsers.parseQueryString( req.getQueryString() );
And an example how Jersey does it (UriComponent):
以及 Jersey 如何做到这一点的示例 (UriComponent):
/**
* Decode the query component of a URI.
*
* @param q the query component in encoded form.
* @param decode true of the query parameters of the query component
* should be in decoded form.
* @return the multivalued map of query parameters.
*/
public static MultivaluedMap<String, String> decodeQuery(String q, boolean decode) {
MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl();
if (q == null || q.length() == 0) {
return queryParameters;
}
int s = 0, e = 0;
do {
e = q.indexOf('&', s);
if (e == -1) {
decodeQueryParam(queryParameters, q.substring(s), decode);
} else if (e > s) {
decodeQueryParam(queryParameters, q.substring(s, e), decode);
}
s = e + 1;
} while (s > 0 && s < q.length());
return queryParameters;
}
private static void decodeQueryParam(MultivaluedMap<String, String> params,
String param, boolean decode) {
try {
int equals = param.indexOf('=');
if (equals > 0) {
params.add(
URLDecoder.decode(param.substring(0, equals), "UTF-8"),
(decode) ? URLDecoder.decode(param.substring(equals + 1), "UTF-8") : param.substring(equals + 1));
} else if (equals == 0) {
// no key declared, ignore
} else if (param.length() > 0) {
params.add(
URLDecoder.decode(param, "UTF-8"),
"");
}
} catch (UnsupportedEncodingException ex) {
// This should never occur
throw new IllegalArgumentException(ex);
}
}
回答by Thomas_
I combine two codes and result is below:
我结合了两个代码,结果如下:
StringBuffer url = request.getRequestURL();
if(request.getQueryString()!= null){
url.append("?");
url.append(request.getQueryString());
}
String url_param = url.toString();
String[] params = url_param.split("&");
String[][] param_key = new String[1320][3];
String[][] param_val = new String[1320][3];
String param = "";
String key_tmp = "";
String val_tmp = "";
y = 1;
for(x=1;x<params.length;x++)
{
param = params[x];
key_tmp = param.substring(0, param.indexOf("="));
param_key[x][y] = key_tmp;
y++;
val_tmp = param.substring(param.indexOf("=") + 1);
param_val[x][y] = val_tmp;
y=1;
}
You get two arrays. One contains key(name), second contains values.
你得到两个数组。一个包含键(名称),第二个包含值。

