我的问题
在我的 android 应用程序中,我从用户那里获取 url 输入,例如“www.google.com”。
我想知道给定的 url 是使用 http
还是 https
。
我尝试过的
在引用了一些 Stack Overflow 问题后,我尝试使用 getScheme()
try {
String url_name="www.google.com";
URI MyUri = new URI(url_name);
String http_or_https="";
http_or_https=MyUri.getScheme();
url_name=http_or_https+"://"+urlname;
Log.d("URLNAME",url_name);
}
catch (Exception e) {
e.printStackTrace();
}
但是我上面的代码抛出了一个异常。
我的问题
上述方法 getScheme() 是否正确?
如果上述方法不正确,如何找到 url http 或 https?
最佳答案
域名就是域名,与协议(protocol)无关。域是 WHERE,协议(protocol)是 HOW。
域是你想去的地方,协议(protocol)是你如何去那里,坐公共(public)汽车、飞机、火车或轮船。问'我想去商店,我怎么问商店我应该坐火车还是坐汽车去?这没有意义。
这在浏览器中起作用的原因是,如果没有提供协议(protocol),浏览器通常会尝试使用 http 和 https 进行连接。
如果您知道 URL,请执行以下操作:
public void decideProtocol(URL url) throws IOException {
if ("https".equals(url.getProtocol())) {
// It is https
} else if ("http".equals(url.getProtocol())) {
// It is http
}
}
关于java - 如何在java中检查给定的域名http或https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597682/