Java 验证 URI 是有效的 http URI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2597653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:41:30  来源:igfitidea点击:

validate that URI is valid http URI

javaurivalidation

提问by Alfred

Solution

解决方案

Okay I found 1 solution on Stackoverflow after a little more searching but I hope to do it with no extra libraries. How to check for a valid URL in Java?

好的,经过多一点搜索,我在 Stackoverflow 上找到了 1 个解决方案,但我希望在没有额外库的情况下做到这一点。如何在 Java 中检查有效的 URL?

My problem:

我的问题:

First of hopefully this is not a duplicate, but I could not find the right answer(right away). I would like to validate that an URI(http) is valid in Java. I came up with the following tests but I can't get them to pass. First I used getPort(), but then http://www.google.nlwill return -1 on getPort(). This are the test I want to have passed

首先希望这不是重复的,但我找不到正确的答案(马上)。我想验证 URI(http) 在 Java 中是否有效。我想出了以下测试,但我无法让它们通过。首先我使用getPort(),但随后http://www.google.nl将返回-1 getPort()。这是我想通过的测试

Test:

测试:

@Test
public void testURI_Isvalid() throws Exception {
    assertFalse(HttpUtils.validateHTTP_URI("ttp://localhost:8080"));
    assertFalse(HttpUtils.validateHTTP_URI("ftp://localhost:8080"));
    assertFalse(HttpUtils.validateHTTP_URI("http://localhost:8a80"));
    assertTrue(HttpUtils.validateHTTP_URI("http://localhost:8080"));
    final String justWrong = 
        "/schedule/get?uri=http://localhost:8080&time=1000000";
    assertFalse(HttpUtils.validateHTTP_URI(justWrong));
    assertTrue(HttpUtils.validateHTTP_URI("http://www.google.nl"));
}

This is what I came up with after I removed the getPort()part but this does not pass all my unit tests.

这是我移除getPort()零件后想到的,但这并没有通过我的所有单元测试。

Production code:

生产代码:

  public static boolean validateHTTP_URI(String uri) {
        final URI u;
        try {
            u = URI.create(uri);
        } catch (Exception e1) {
            return false;
        }
        return "http".equals(u.getScheme());
  }

This is the first test that is failing because I am no longer validating the getPort() part. Hopefully somebody can help me out. I think I am not using the right class to validate URLs?

这是第一个失败的测试,因为我不再验证 getPort() 部分。希望有人可以帮助我。我想我没有使用正确的类来验证 URL?

P.S:

PS:

I don't want to connect to the server to validate the URI is correct. At least not yet in this step. I only want to validate scheme.

我不想连接到服务器来验证 URI 是否正确。至少在这一步还没有。我只想验证方案。

采纳答案by Alfred

Code that will PASS

将通过的代码

public static boolean validateHTTP_URI(String uri) {
    final URL url;
    try {
        url = new URL(uri);
    } catch (Exception e1) {
        return false;
    }
    return "http".equals(url.getProtocol());
}

My next Question is:

我的下一个问题是:

I heard/read(Joshua Bloch I believe) somewhere that Url does not work properly if you don't have internet(anymore). But I don't think that's true(anymore)? Could someone please elaborate.

我听说/读过(我相信是 Joshua Bloch)如果你没有互联网(不再),Url 就不能正常工作。但我不认为那是真的(不再)?有人可以请详细说明。

回答by Tom

You could try to use this regular expression:

您可以尝试使用此正则表达式:

(?:(?<protocol>http(?:s?)|ftp)(?:\:\/\/)) (?:(?<usrpwd>\w+\:\w+)(?:\@))? (?<domain>[^/\r\n\:]+)? (?<port>\:\d+)? (?<path>(?:\/.*)*\/)? (?<filename>.*?\.(?<ext>\w{2,4}))? (?<qrystr>\??(?:\w+\=[^\#]+)(?:\&?\w+\=\w+)*)* (?<bkmrk>\#.*)?

This will tell you if an URL is valid and it will give you the protocolvalue. I don't know Java, so I don't know what class you need to use to validate Regular Expressions.

这将告诉您 URL 是否有效,并为您提供protocol值。我不懂 Java,所以我不知道您需要使用什么类来验证正则表达式。