从http get请求中读取非英语字符
我在从http获取请求中获取希伯来字符时遇到问题。
我得到这样的正方形字符:“[]”而不是希伯来字符。
英文字符很好。
这是我的function:
public String executeHttpGet(String urlString) throws Exception { BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(urlString)); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8")); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String page = sb.toString(); // System.out.println(page); return page; } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
您可以通过此示例测试url:
String str = executeHttpGet("http://kavim-t.co.il/include/getXMLStations.asp?parent=7_%20_1");
谢谢!
您链接的文件似乎不是UTF-8
。 我测试它使用WINDOWS-1255
(希伯来语编码)正确打开,你应该尝试而不是UTF-8
。
尝试使用其他网站,看起来它不使用UTF-8。 或者,UTF-16 可以工作,但我没有尝试过。 你的代码看起来很好。
正如其他人所指出的那样,内容实际上并没有编码为UTF-8。 您可能希望查看httpEntity.getContentType()
以提取内容的实际编码,然后将其传递给InputStreamReader
。 这意味着您的代码将能够正确处理任何编码。
嗨,因为发布在这个其他问题PHP / MySQL中的特殊字符
您可以在他们设置utf-8的示例中设置php文件中的字符,但您可以设置支持所需字符集的其他类型。