在Java中,我需要使用HTTP Post向服务器发送请求,但如果URL参数包含某些特殊字符,则会抛出以下异常:
java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "&'"
用于发送数据的代码:
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ああ#$%&'(<>?/.,あああああ],它就会抛出异常。我想知道是否有人能够给我一些提示,让我能够编码和解码特殊字符?
非常感谢您提前的帮助。