java 检查URL是HTTPS还是HTTP协议?

0mkxixxg  于 2022-10-30  发布在  Java
关注(0)|答案(8)|浏览(481)

我目前正在使用以下代码从android docs herehere中读取文件。(在设置屏幕中)如果他们的网站使用HTTP或HTTPS协议。如果他们的网站使用HTTP协议,那么它对HttpURLConnectionHttpsURLConnection都有效,但如果他们的网站使用HTTPS协议,那么它就不能在HttpURLConnection协议下工作,而且最糟糕的是它不会给予我一个异常错误。
因此,从本质上讲,我如何检查Web 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://**前缀,我如何知道哪个是正确的?

gc0ot86w

gc0ot86w1#

您可以使用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.
i7uaboj4

i7uaboj42#

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

rnmwe5a23#

请尝试以下代码:

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

zzzyeukh4#

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

public static boolean isHttpOrHttpsUrl(String url) {
        return url.matches("^(http|https|ftp)://.*$");
    }
axzmvihb

axzmvihb5#

这可以通过使用Util进行检查。

  • 如果URL是http:URL中。
  • 如果URL是https:URL中。
bkkx9g8r

bkkx9g8r6#

你可以试试这个

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

xqk2d5yq7#

我认为这是可行的,至少看起来是可行的,你们觉得呢?我把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?来覆盖所有成功的连接吗?

zbdgwd5y

zbdgwd5y8#

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

public void testUrl(Object urlHttp){

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

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

此致

相关问题