【问题标题】:Check if URL is HTTPS or HTTP protocol?检查 URL 是 HTTPS 还是 HTTP 协议?
【发布时间】:2015-08-05 07:46:49
【问题描述】:

我目前正在使用以下内容从 android 文档 herehere 中读取文件。用户选择(在设置屏幕中)他们的站点是否使用 HTTP 或 HTTPS 协议。如果他们的网站使用 HTTP 协议,那么它适用于 HttpURLConnectionHttpsURLConnection,但如果他们的网站使用 HTTPS 协议,那么它不适用于 HttpURLConnection 协议,最糟糕的是它不会给我一个例外错误。下面是我正在使用的示例代码。

所以本质上,我如何检查网页 URL 是否为 HTTPS 协议,从而检查用户是否选择了正确的协议?

InputStream inputStream;
HttpURLConnection urlConnection;
HttpsURLConnection urlHttpsConnection;
boolean httpYes, httpsYes; 
try {
if (httpSelection.equals("http://")) {
  URL url = new URL(weburi);
  urlConnection = (HttpURLConnection) url.openConnection();
  inputStream = new BufferedInputStream((urlConnection.getInputStream()));
httpYes = True;
}
if (httpSelection.equals("https://")) {
  URL url = new URL(weburi);
  urlHttpsConnection = (HttpsURLConnection) url.openConnection();
  urlHttpsConnection.setSSLSocketFactory(context.getSocketFactory());
  inputStream = urlHttpsConnection.getInputStream();
  https=True;
}

catch (Exception e) {
//Toast Message displays and settings intent re-starts
}
finally {
  readFile(in);
if(httpYes){ 
    urlConnection.disconnect();
    httpYes = False;
 }
if(httpsYes){ 
    urlHttpsConnection.disconnect();
    httpsYes = False;
 } 
}
}

编辑:

详细说明。我需要查看它是否从网站返回有效响应?因此,如果用户选择 http 而不是 https,我该如何检查 http 是否是不正确的前缀/协议?

如何检查网站是使用 HTTPS 还是 HTTP 协议?如果用户只输入 www.google.com 并且我在其上附加 https://http:// 前缀,我怎么知道哪一个是正确的?

【问题讨论】:

  • 也许我错过了什么。你不能检查 URL 字符串是否以 https://http// 开头?
  • 是的,我可以@Karim,但是如果用户选择了错误的字符串会怎样?这就是我遇到的问题。
  • 在所有情况下都使用HttpsURLConnection,或者不检查httpSelection.equals(...),而是立即检查URL是否以https://http://开头,并且不要要求用户选择任何内容.
  • @Karim 我认为这可能是迄今为止最好的解决方案。无论用户选择了什么https://http://,它都将始终有效。虽然我不确定这是否是正确的方法。
  • @Karim 我刚刚意识到它仍然无法正常工作,因为当 URL 是 https 并且用户输入 http 时,它会给我和以前一样的结果。

标签: java android http https


【解决方案1】:

你可以使用androidURLUtil来检查url是HTTP还是HTTPS:

public static boolean isHttpUrl (String url)
Returns True iff the url is an http: url.

public static boolean isHttpsUrl (String url) 
Returns True iff the url is an https: url.

编辑:

public static boolean isValidUrl (String url)
Returns True iff the url is valid.

【讨论】:

  • 以上方法我都试过了。即使它正确且有效,但如果用户使用 http 而不是 https,它并不能解决问题,它仍然返回有效的响应。
  • 这实际上是否尝试传输任何数据,或者如果连接以 https:// 开头,它只会返回 true
  • @Dino 不确定,但可能是您的 HTTP URL 正在重定向到 HTTPS。
  • @DhavalPatel 我不确定它是否正在尝试重定向,但如果用户输入不正确的前缀,我不会收到异常错误。它返回真。我在 HTTP 和 HTTPS 前缀上都试过了。
  • @DhavalPatel,我刚刚使用了getResponseCode(),它确实为 HTTPS 返回 302(这意味着重定向)
【解决方案2】:
URLConnection result = url.openConnection();
if (result instanceof HttpsURLConnection) {
   // https
}
else if (result instanceof HttpURLConnection) {
   // http
}
else {
  // null or something bad happened
}

【讨论】:

    【解决方案3】:

    我已经检查了URLUtil 类并检查了它是否包含用于此类东西的这么多方法,但我只是扩展了答案,您也可以简单地执行以下操作:-

    public static boolean isHttpOrHttpsUrl(String url) {
            String patter = "^(http|https|ftp)://.*$";
            if (url.matches(patter)){
                return true;
            }else{
                return false;
            }
        }
    

    【讨论】:

      【解决方案4】:

      试试这个代码:

      mConnexion = (URLUtil.isHttpsUrl(mStringUrl)) ? (HttpsURLConnection) url.openConnection() : (HttpURLConnection) url.openConnection();
      

      【讨论】:

        【解决方案5】:

        这可以通过使用Util来检查。

        • isHttpUrl 返回 True 如果 url 是 http: url。
        • isHttpsUrl 返回 True 如果 url 是 https: url。

        【讨论】:

        • @Kartheek 和我的回答不一样吗?
        • 我没有检查你的答案@DhavalPatel
        【解决方案6】:

        你可以试试这个

            boolean httpYes, httpsYes;
             try {
        
              URL url = new URL(weburi);
              urlConnection = (HttpURLConnection) url.openConnection();
              inputStream = new BufferedInputStream((urlConnection.getInputStream()));
            httpYes = True;
            }
        
        
            catch (Exception e) {
            //Toast Message displays and settings intent re-starts
                  URL url = new URL(weburi);
                  urlHttpsConnection = (HttpsURLConnection) url.openConnection();
                  urlHttpsConnection.setSSLSocketFactory(context.getSocketFactory());
                  inputStream = urlHttpsConnection.getInputStream();
                  https=True;
            }
        

        【讨论】:

        • @Tony 为什么要添加额外的 try-catch 开销?如果我们可以通过简单的 if-else 条件来检查它?
        • 我 100% 支持你,但它可以作为额外的场景添加。顺便说一下,在两种情况下都为 http 连接添加了 try catch 语句
        【解决方案7】:

        我的建议是创建函数表达式正则。 例如:

        public void testUrl(Object urlHttp){
        
            String url = "https://www.google.com";
        
            if(url.matches("^(https?)://.*$")){
             Object o = (HttpsURLConnection) urlHttp;
            }else{
             Object o = (HttpURLConnection) urlHttp;
            }
        }
        

        问候

        【讨论】:

        • 它不处理您没有在 URL 中指定协议的情况。
        • 无论如何使用这个正则表达式,它会将对象转换为 https,即使它不是
        【解决方案8】:

        我认为这行得通,至少它似乎行得通,你们怎么看?我将这个 if 语句放在 httpsYes = TruehttpYes = True 之前。

        It seems that when the HTTPS protocol is selected it wants to redirect using response code 302, but for all other instances it connects with response code 200. I throw a new ConnectionException() error as that takes the user back to the settings屏幕以更正 URL 错误。

        对于 HTTPS 协议:

        if (httpsURLConnection.getResponseCode() != 200) {
             throw new ConnectException();
        }
        

        对于 HTTP 协议:

        if (urlConnection.getResponseCode() != 200) {
             throw new ConnectException();
        }
        

        评论?我应该使用urlConnection.getResponseCode() > 199 && < 300 吗?涵盖所有成功的连接?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-24
          • 1970-01-01
          • 2014-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多