2

需要发送带有西里尔字符的 http 请求。在字符串“文本”中,我有一个俄语单词。它编码了最终地址,但 connection.getInputStream() 抛出异常。用英文单词它可以正常工作。编码的 URL(地址)通常在浏览器中打开。这里有一个代码:

    String text = params[0] ;
    try {
        text = URLEncoder.encode(text, HTTP.UTF_8) ;
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace() ;
    }
    String address = "http://"+text+".jpg.to" ;
    String result = "" ;
    BufferedReader reader = null ;
    try {
        URL url = new URL(address) ;
        URLConnection connection = url.openConnection() ;
        InputStream stream = connection.getInputStream() ;
        InputStreamReader inReader = new InputStreamReader(stream, "UTF-8") ;
        reader = new BufferedReader(inReader) ;

        String inputLine = null ;
        while ((inputLine = reader.readLine()) != null) {
            result = result+inputLine ;
        }
    } catch (Exception e) {
        e.printStackTrace() ;
    } finally {        
        try {
            reader.close() ;
        } catch (Exception e) {
            e.printStackTrace() ;
        } 
    }
4

0 回答 0