检查URL是HTTPS还是HTTP协议?

时间:2015-05-23 09:27:28

标签: java android http https

我目前正在使用以下内容从Android文档herehere中读取文件。用户选择(在设置屏幕中)他们的站点是使用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:// 前缀,我怎么知道哪一个是正确的?

8 个答案:

答案 0 :(得分:9)

您可以使用android URLUtil检查网址是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.

答案 1 :(得分:4)

URLConnection result = url.openConnection();
if (result instanceof HttpsURLConnection) {
   // https
}
else if (result instanceof HttpURLConnection) {
   // http
}
else {
  // null or something bad happened
}

答案 2 :(得分:2)

可以使用Util来检查。

    如果网址为http:url。,则
  • isHttpUrl返回True 如果网址为https:url。
  • ,则
  • isHttpsUrl返回True

答案 3 :(得分:2)

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

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

答案 4 :(得分:0)

你可以试试这个

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

答案 5 :(得分:0)

试试这段代码:

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

答案 6 :(得分:-2)

我觉得这很有效,至少看起来有效,你们觉得怎么样?我将此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吗?覆盖所有成功的连接?

答案 7 :(得分:-2)

我的建议是定期创建函数表达式。 例如:

public void testUrl(Object urlHttp){

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

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

此致