如何检查给出的java域名HTTP或HTTPS? [英] How to check given domain name http or https in java?

查看:1518
本文介绍了如何检查给出的java域名HTTP或HTTPS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题

在我的Andr​​oid应用程序,我得到的网址输入来自用户,如www.google.com。

In my android application I get url input from user, like "www.google.com".

我想找出给定的URL是否使用 HTTP HTTPS

I want to find out for the given url whether to use http or https.

我已经试过

指的是一些堆栈溢出问题,我试着用后的 getScheme()

after referring to some Stack Overflow questions I tried with 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();
}

但我上面code会抛出异常。

But my above code throws an exception.

我的提问

是上面的方法getScheme()正确与否?

如果上面的方法不正确,如何找到URL HTTP或HTTPS?

If above approach is incorrect, how to find url http or https?

推荐答案

一个域名是域名,它没有任何关系的协议。域名是在哪里,该协议是怎么样。

A domain name is a domain name, it has nothing to do with protocol. The domain is the WHERE, the protocol is the HOW.

域名你想要去的位置,该协议是你怎么去那里,巴士,飞机,火车或乘船。这是没有意义的问我想去店里,我怎么问我是否应该去乘坐火车或汽车专卖店?

The domain is the location you want to go, the protocol is how do you go there, by bus, by plane, by train or by boat. It makes no sense to ask 'I want to go to the store, how do I ask the store if I should go by train or by car?'

这部作品在浏览器的原因是,浏览器通常会尝试使用HTTP和HTTPS,如果没有协议提供连接。

The reason this works in browsers is that the browser usually tries to connect using both http and https if no protocol is supplied.

如果你知道网址,做到这一点:

If you know the URL, do this:

    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域名HTTP或HTTPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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