gpt4 book ai didi

java - 西类牙字符和 URISyntaxException

转载 作者:行者123 更新时间:2023-11-30 06:29:52 26 4
gpt4 key购买 nike

我正在研究西类牙语版本的搜索,每当用户输入西类牙语字符(比如 HÍBRIDOS)时,我都会看到一些异常(如下所示)。在下面显示我的编码方式。发送的 url 是通过线路发送的,如图所示。

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);

最佳答案

如果您根据请求(GET 请求)发送特殊字符,则必须对它们进行 URL 转义。查看此线程以了解操作方法。 HTTP URL Address Encoding in Java

收到请求后,必须做逆向处理才能得到原词。

关于java - 西类牙字符和 URISyntaxException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11052941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com