检查 URL 是 HTTPS 还是 HTTP 协议?

Check if URL is HTTPS or HTTP protocol?

我目前正在使用以下内容从 android 文档 here and here 中读取文件。用户选择(在设置屏幕中)他们的站点是使用 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;
 } 
}
}

编辑:

详细说明一下。我需要查看它是否 returns 来自网站的有效响应?因此,如果用户选择了 http 而不是 https,我该如何检查 http 是否不正确 prefix/protocol?

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

您可以使用 android URLUtil 检查 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.
URLConnection result = url.openConnection();
if (result instanceof HttpsURLConnection) {
   // https
}
else if (result instanceof HttpURLConnection) {
   // http
}
else {
  // null or something bad happened
}

这可以使用 Util 进行检查。

  • isHttpUrl returns 正确当且仅当 url 是一个 http: url.
  • isHttpsUrl returns 正确当且仅当 url 是一个 https:url.

你可以试试这个

    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;
    }

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

似乎选择HTTPS协议时,它想要使用响应代码302重定向,但是对于所有其他实例,它都与响应代码200。我抛出了一个新的ConnectionException()错误,因为这使用户带来了错误返回设置屏幕以更正 URL 错误。

对于 HTTPS 协议:

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

对于 HTTP 协议:

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

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

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

public void testUrl(Object urlHttp){

    String url = "https://www.google.com";

    if(url.matches("^(https?)://.*$")){
     Object o = (HttpsURLConnection) urlHttp;
    }else{
     Object o = (HttpURLConnection) urlHttp;
    }
}

此致

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

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

试试这个代码:

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