我正在研究西班牙语版本的搜索,当用户输入西班牙语字符(比如HÍBRIDOS)时,我会看到一些异常(如下所示)。显示我如何在下面编码。 网址发送的网址如图所示。
url=http://wwwdev.searchbridg.com/absd/JSONControllerServlet.do?&N=0&Ntk=AllText&Ntt=HÃBRIDOS&Nty=1&Ntx=mode+matchall
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
try {
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
HttpHost proxy = new HttpHost(getProxy(), getProxyPort());
ConnRouteParams.setDefaultProxy(params, proxy);
URI uri;
InputStream data = null;
uri = new URI(url);
HttpGet method = new HttpGet(uri);
HttpResponse response=null;
try {
response = httpClient.execute(method);
}catch(Exception e) {
e.printStackTrace();
throw e;
}
data = response.getEntity().getContent();
Reader r = new InputStreamReader(data);
HashMap<String, Object> jsonObj = (HashMap<String, Object>) GenericJSONUtil.fromJson(r);
java.net.URISyntaxException: Illegal character in query at index 101: http://wwwdev.searchbridge.com/abs/JSONControllerServlet.do?&N=0&Ntk=AllText&Ntt=H├?BRIDOS&Nty=1&Ntx=mode+matchall
at java.net.URI$Parser.fail(URI.java:2816)
at java.net.URI$Parser.checkChars(URI.java:2989)
at java.net.URI$Parser.parseHierarchical(URI.java:3079)
at java.net.URI$Parser.parse(URI.java:3021)
at java.net.URI.<init>(URI.java:578)
我尝试使用UTF-8编码进行编码但仍无法正常工作显示相同的异常。 html页面设置为<meta charset="utf-8" />
byte[] bytes = url.getBytes("UTF8");
String stringuRL = new String(bytes,"UTF-8");
uri = new URI(stringuRL);
答案 0 :(得分:4)
如果您根据请求发送特殊字符(GET请求),则必须对它们进行URLescape。看看这个帖子,了解如何。 HTTP URL Address Encoding in Java
当您收到请求时,您必须执行相反的过程才能获得原始单词。
答案 1 :(得分:1)
get请求中的所有参数都需要对其值进行编码。
如果您使用的是HTTPClient 4,您可以或多或少地这样做:
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("parameter_name_Ã", "another value with ~ãé"));
parameters.add(new BasicNameValuePair("second_parameter", "still other ú û"));
String url = "http://foo.bar/?" + URLEncodedUtils.format(parameters, "UTF-8");
此案例的结果为http://foo.bar/?parameter_name_%C3%83=another+value+with+%7E%C3%A3%C3%A9&second_parameter=still+other+%C3%BA+%C3%BB