URL编码和解码Java中的特殊字符
在Java中,我需要使用HTTP Post向服务器发送请求,但是如果URL的参数中包含一些特殊字符,它会抛出下面的异常
java.lang.IllegalArgumentException: URLDecoder:非法十六进制字符 转义 (%) 模式 - 对于输入字符串: “&'”
发送数据的代码
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
String sessionId = RequestUtil.getRequest().getSession().getId();
String data = arg.getData().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(param1, data));
params.add(new BasicNameValuePair(param2, sessionId));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = (HttpResponse) httpclient.execute(httpPost);
在服务器端,我使用下面的代码来读取信息
String data = request.getParameter(param1);
if (data != null) {
actionArg = new ChannelArg(URLDecoder.decode(data, "UTF-8"));
}
该代码工作正常,但如果我输入一些特殊字符,例如 [aああ#$%&'(<> 以便能够编码和解码特殊字符?
?/.,あああああ],它会抛出异常。我想知道是否有人可以帮助我一些提示,
In Java, I need to use HTTP Post to send request to server, but if in the parameter of the URL contains some special character it throws below Exception
java.lang.IllegalArgumentException:
URLDecoder: Illegal hex characters in
escape (%) pattern - For input string:
"&'"
The code to send data
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
String sessionId = RequestUtil.getRequest().getSession().getId();
String data = arg.getData().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(param1, data));
params.add(new BasicNameValuePair(param2, sessionId));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = (HttpResponse) httpclient.execute(httpPost);
And at the server side, i use the below code to read information
String data = request.getParameter(param1);
if (data != null) {
actionArg = new ChannelArg(URLDecoder.decode(data, "UTF-8"));
}
The code works correctly but if i input some special character like [aああ#$%&'(<>?/.,あああああ], it will throw exception. I wonder if someone could help me some hint to be able to encode and decode special characters?
Thank you very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对文本进行编码以安全地通过互联网:
并解码:
To encode text for safe passage through the internets:
And to decode:
遗憾的是 url 编码器无法解决您的问题。我遇到了这个问题并使用了自定义实用程序。我记得这是我通过谷歌搜索得到的;)。
http://www.javapractices.com/topic/TopicAction.do?Id=96
Sadly url encoder will not solve your problem. I had this problem and used a custom utility. I remember this I got from googling ;).
http://www.javapractices.com/topic/TopicAction.do?Id=96
如果这是 servlet API,参数已经解码。不需要进一步处理百分比编码。
我没有使用 HttpClient,但确保它在标头中发送编码:
或者,如果必须,请在任何
getParameter
调用之前设置已知编码:If this is the servlet API, the parameters have already been decoded. No further handling of percent-encoding is necessary.
I haven't used HttpClient, but ensure it is sending the encoding in the header:
Or, if you must, set the known encoding before any
getParameter
calls:尝试番石榴
使用 com.google.common.net.UrlEscapers
它可以很好地与中文一起
使用,如下所示:
try guava
use com.google.common.net.UrlEscapers
it works fine with chinese
like this: