URL编码和解码Java中的特殊字符

发布于 2024-10-14 16:36:13 字数 1106 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三人与歌 2024-10-21 16:36:13

对文本进行编码以安全地通过互联网:

import java.net.*;
...
try {
    encodedValue= URLEncoder.encode(rawValue, "UTF-8");
} catch (UnsupportedEncodingException uee) { }

并解码:

try {
    decodedValue = URLDecoder.decode(rawValue, "UTF-8");
} catch (UnsupportedEncodingException uee) { }

To encode text for safe passage through the internets:

import java.net.*;
...
try {
    encodedValue= URLEncoder.encode(rawValue, "UTF-8");
} catch (UnsupportedEncodingException uee) { }

And to decode:

try {
    decodedValue = URLDecoder.decode(rawValue, "UTF-8");
} catch (UnsupportedEncodingException uee) { }
欢烬 2024-10-21 16:36:13

遗憾的是 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

陌上芳菲 2024-10-21 16:36:13
String data = request.getParameter(param1);

如果这是 servlet API,参数已经解码。不需要进一步处理百分比编码。


我没有使用 HttpClient,但确保它在标头中发送编码:

Content-type: application/x-www-form-urlencoded; charset=UTF-8

或者,如果必须,请在任何 getParameter 调用之前设置已知编码:

request.setCharacterEncoding("UTF-8");
String data = request.getParameter(param1);

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:

Content-type: application/x-www-form-urlencoded; charset=UTF-8

Or, if you must, set the known encoding before any getParameter calls:

request.setCharacterEncoding("UTF-8");
当爱已成负担 2024-10-21 16:36:13

尝试番石榴

使用 com.google.common.net.UrlEscapers

它可以很好地与中文一起

使用,如下所示:

Escaper escaper = UrlEscapers.urlFragmentEscaper();
String result = escaper.escape(yoururl);

try guava

use com.google.common.net.UrlEscapers

it works fine with chinese

like this:

Escaper escaper = UrlEscapers.urlFragmentEscaper();
String result = escaper.escape(yoururl);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文