【问题标题】:URL with é (e acute)带有 é 的 URL(e 锐音)
【发布时间】:2015-06-04 16:34:34
【问题描述】:

我的网址格式有问题。我想从 url 发出一个 get 请求: 'http://www.radiobébé.com/titre.txt'(http://www.radiobebe.com/titre.txt 不存在)

String responseBody = "";

    try {
        URL u = new URL("http://www.radiobébé.com/titre.txt");
        HttpURLConnection urlConnection = (HttpURLConnection) u
                .openConnection();

        urlConnection.setRequestMethod("GET");

        urlConnection.connect();

        int statusCode = urlConnection.getResponseCode();

        if (statusCode == HttpURLConnection.HTTP_OK) {
            responseBody = urlConnection.getInputStream().toString();
        }
    } catch (IOException e) {
        Log.e("RadioBébé", "failed to get song title : " + e.toString());
    }

使用这个函数我得到错误'java.net.UnknownHostException'

我试过了:

String url = "http://www." + URLencoder.encoder("radiobébé", "UTF-8") + ".com/titre.txt";

Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
    .authority("www.radiobébé.com")
    .appendPath("titre.txt");

同样的问题....(主机可能不为空)

你知道我该如何解决吗?

【问题讨论】:

    标签: android uri httpurlconnection urlencode


    【解决方案1】:

    试试这个网址:http://www.xn--radiobb-gyab.com/titre.txt

    当我在浏览器中检查连接时,这不是http://www.radiobébé.com/titre.txt,而是这个网址。

    【讨论】:

    • 感谢您的帮助,但我必须使用基地址,因为重定向可以更改
    【解决方案2】:

    我找到了解决办法,我们可以使用 IDN:

    String url = IDN.toASCII("http://www.radiobébé.com/titre.txt");
    

    结果:

    网址 = http://www.xn--radiobb-gyab.com/titre.txt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 2018-11-04
      相关资源
      最近更新 更多