java中如何获取url中的值

29次阅读
没有评论

共计 734 个字符,预计需要花费 2 分钟才能阅读完成。

在 Java 中,可以使用 java.net.URL 类来获取 URL 中的值。以下是一个简单的示例代码:

import java.net.URL;
import java.net.MalformedURLException;

public class Main {public static void main(String[] args) {try {URL url = new URL("http://example.com/path?param1=value1&param2=value2");

            String protocol = url.getProtocol();
            String host = url.getHost();
            int port = url.getPort();
            String path = url.getPath();
            String query = url.getQuery();

            System.out.println("Protocol: " + protocol);
            System.out.println("Host: " + host);
            System.out.println("Port: " + port);
            System.out.println("Path: " + path);
            System.out.println("Query: " + query);
        } catch (MalformedURLException e) {System.out.println("Invalid URL");
        }
    }
}

在这个例子中,我们创建了一个 URL 对象并传入一个 URL 字符串。然后使用 getProtocol()getHost()getPort()getPath()getQuery()方法来获取 URL 中的协议、主机、端口、路径和查询参数。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-04-13发表,共计734字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)