提取完整的URL基础URL [英] Extract base url from full url

查看:283
本文介绍了提取完整的URL基础URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何快速的方法来提取基准URL的完整网址?对于例如,如果我有 http://test.example.com/abcd/test.html - 我只想 http://test.example.com

Any quick way to extract base url from full url? for e.g., if i have http://test.example.com/abcd/test.html - i want only http://test.example.com.

我总是可以做字符串解析 - 但想知道是否有东西在乌里在那里我可以直接得到它。

I can always do string parsing - but wanted to know if there is something in Uri where I can get it directly.

推荐答案

什么:

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

try
{
  URL url = new URL("http://test.example.com/abcd/test.html");
  String baseUrl = url.getProtocol() + "://" + url.getHost();
}
catch (MalformedURLException e)
{
  // do something
}

这篇关于提取完整的URL基础URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆