【问题标题】:How to Perform MalformedURLException / 404 Not Found如何执行 MalformedURLException / 404 Not Found
【发布时间】:2019-03-24 12:49:41
【问题描述】:

如何让我的程序返回 MalformedUrlException 而不仅仅是一般的异常?

我正在制作一个简单的函数,它读取用户在控制台中输入的 URL,并从 URL 返回内容。我需要它来检查 URL 是有效的 URL 还是不是有效的 URL。

示例网址: http://google.com/not-found.html http://google.com

我创建了两个捕获异常,但似乎总是返回整体异常而不是 MalformedUrlException。

    public static String getUrlContents(String theUrl) {
    String content = "";
    try {
        URL url = new URL(theUrl);
        //Create a url connection object 
        URLConnection urlConnection = url.openConnection();
        //wrap the url connection a buffered reader 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String line;
        while((line = bufferedReader.readLine()) != null) {
            content += line + "\n";
        }
        bufferedReader.close();
    } catch (MalformedURLException e) {
        System.out.println("The following url is invalid'" + theUrl + "'");
        //logging error should go here
    } catch (Exception e) {
        System.out.println("Something went wrong, try agian");
    }
    return content;
}

【问题讨论】:

  • docs.oracle.com/javase/8/docs/api/java/net/…: 抛出:MalformedURLException - 如果未指定协议,或发现未知协议,或规范为空。
  • 您能否提供一个带有不良行为的输入示例?
  • @tworogue 如果我输入 http:/google.com 或 google.com/not-found.html 我希望它抛出 MalformedURlException
  • @AhmeeyaGoldman 看看我的回答。

标签: java http exception get malformedurlexception


【解决方案1】:

首先,java.net.MalformedURLException 不是“未找到”资源的情况:

公共类 MalformedURLException 扩展 IOException

抛出表示出现了格式错误的 URL。要么不合法 协议可以在规范字符串中找到,或者该字符串可以 不被解析。

我了解您希望了解 URL 导致未找到返回代码 (404) 的情况。为此,您需要检查 HTTP 响应代码。

最简单的方法是使用java.net.HttpURLConnection

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/HttpURLConnection.html

公共抽象类 HttpURLConnection 扩展了 URLConnection

支持 HTTP 特定功能的 URLConnection。见规格 了解详情。

每个 HttpURLConnection 实例用于发出单个请求,但 与 HTTP 服务器的底层网络连接可能是 由其他实例透明共享。调用 close() 方法 在 HttpURLConnection 的 InputStream 或 OutputStream 之后 请求可能会释放与此实例关联的网络资源,但 对任何共享持久连接没有影响。调用 disconnect() 方法可能会关闭底层套接字,如果持久 那时连接处于空闲状态。

您可以通过调用getResponseCode() 来查看响应代码。如果结果小于 400,则得到有效响应,否则为客户端错误 (4xx) 或服务器错误 (5xx)。

类似这样的:

public static String getUrlContents(String theUrl) {
    String content = "";
    try {
        URL url = new URL(theUrl);
        //Create a url connection object 
        URLConnection urlConnection = url.openConnection();
        if (urlConnection instanceof HttpURLConnection) {
            HttpURLConnection conn = (HttpURLConnection) urlConnection;
            if (conn.getResponseCode() < 400) {
                // read contents
            } else {
                System.out.println(conn.getResponseMessage());
                // treat the error as you like
            }
        } else {
            // not a HTTP connection, treat as you like
        }
    } catch (MalformedURLException e) {
        System.out.println("The following url is invalid'" + theUrl + "'");
        //logging error should go here
    } catch (Exception e) {
        System.out.println("Something went wrong, try agian");
    }
    return content;
}

我没有检查代码,但我认为您可以大致了解。

【讨论】:

    猜你喜欢
    • 2015-06-27
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2019-05-12
    • 2020-08-19
    • 2017-05-11
    • 2016-03-16
    相关资源
    最近更新 更多