【问题标题】:In java, what's the best way to read a url and split it into its parts?在 java 中,读取 url 并将其拆分为各个部分的最佳方法是什么?
【发布时间】:2013-02-25 21:19:59
【问题描述】:

首先,我知道还有其他类似的帖子,但由于我使用的是 URL,而且我并不总是确定我的分隔符是什么,所以我觉得我可以发布我的问题。我的任务是制作一个粗糙的网络浏览器。我有一个文本字段,用户可以在其中输入所需的 URL。然后,我显然必须导航到该网页。这是我老师的一个例子,我的代码看起来有点像。这是我应该发送到我的套接字的代码。示例网址:http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

 GET /wiki/Hypertext_Transfer_Protocol HTTP/1.1\n
Host: en.wikipedia.org\n
\n

所以我的问题是:我将在 url 中读取为一个完整的字符串,那么如何仅提取“en.wikipedia.org”部分和扩展名?我试过这个作为测试:

 String url = "http://en.wikipedia.org/wiki/Hypertext Transfer Protocol";
    String done = " ";
    String[] hope = url.split(".org");

    for ( int i = 0; i < hope.length; i++)
    {
        done = done + hope[i];
    }
    System.out.println(done);

这只是打印出没有“.org”的 URL。我认为我在正确的轨道上。我只是不确定。另外,我知道网站可以有不同的结尾(.org、.com、.edu 等),所以我假设我必须有一些 if 语句来补偿可能的不同结尾。基本上,我如何将 url 分成我需要的两个部分?

【问题讨论】:

标签: java string url split


【解决方案1】:

URL 类几乎可以做到这一点,看看tutorial。例如,给定这个 URL:

http://example.com:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING

这是您可以期望获得的信息:

protocol = http
authority = example.com:80
host = example.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING

【讨论】:

  • 太棒了!谢谢。我真的需要更好地了解我的课程
  • @GabrielleLee 不客气!如果这个答案对您有帮助,请不要忘记点击左侧的复选标记接受它;)
【解决方案2】:

这就是你应该如何分割你的 URL 部分:http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

【讨论】:

    【解决方案3】:

    即使URL 类的答案很好,这里还有一种使用 REGEXP 将 URL 拆分为组件的方法:

    "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"
          ||            |  |          |       |   |        | |
          12 - scheme   |  |          |       |   |        | |
                        3  4 - authority, includes hostname/ip and port number.
                                      5 - path|   |        | |
                                              6   7 - query| |
                                                           8 9 - fragment
    

    您可以将它与Pattern 类一起使用:

    var regex = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
    var pattern = Pattern.compile(REGEX);
    var matcher = pattern.matcher("http://example.com:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING");
    if (matcher.matches()) {
      System.out.println("scheme: " + matcher.group(2));
      System.out.println("authority: " + matcher.group(4));
      System.out.println("path: " + matcher.group(5));
      System.out.println("query: " + matcher.group(7));
      System.out.println("fragment: " + matcher.group(9));
    }
    

    【讨论】:

      【解决方案4】:

      代替url.split(".org"); 尝试url.split("/"); 并遍历您的字符串数组。

      或者您可以查看正则表达式。 This is a good example 开始。

      祝你作业顺利。

      【讨论】:

      • 使用专门设计的 URL。它将处理许多通过简单拆分或正则表达式难以处理的边缘情况。
      【解决方案5】:

      您可以使用 String 类 split() 并将结果存储到 String 数组中,然后迭代数组并将变量和值存储到 Map 中。

      public class URLSPlit {
          public static Map<String,String> splitString(String s) {
              String[] split = s.split("[= & ?]+");
              int length = split.length;
              Map<String, String> maps = new HashMap<>();
      
              for (int i=0; i<length; i+=2){
                    maps.put(split[i], split[i+1]);
              }
      
              return maps;
          }
      
          public static void main(String[] args) {
              String word = "q=java+online+compiler&rlz=1C1GCEA_enIN816IN816&oq=java+online+compiler&aqs=chrome..69i57j69i60.18920j0j1&sourceid=chrome&ie=UTF-8?k1=v1";
              Map<String, String> newmap =  splitString(word);
      
              for(Map.Entry map: newmap.entrySet()){
                  System.out.println(map.getKey()+"  =  "+map.getValue());
              }
          }
      }
      

      【讨论】:

      • 使用专门设计的 URL。它将处理许多通过简单拆分或正则表达式难以处理的边缘情况。
      猜你喜欢
      • 2020-11-21
      • 2019-11-21
      • 2011-08-29
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多