繁体   English   中英

检查URL是HTTPS还是HTTP协议?

[英]Check if URL is HTTPS or HTTP protocol?

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

所以从本质上讲,我如何检查网址是否为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://前缀,我怎么知道哪一个是正确的?

您可以使用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检查。

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

我检查了URLUtil类,并检查它包含了很多这类东西的方法,但我只是扩展你可以简单地做的答案如下: -

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

你可以试试这个

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

试试这段代码:

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

我觉得这很有效,至少看起来有效,你们觉得怎么样? 我把这个if语句放在httpsYes = TruehttpYes = True

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

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

问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM