判断一个Url是不是有效的Url

直接在自己的类中引入如下工具类,将需要判断的url作为构造函数的参数传递进去即可,如果是有效的url会抛出异常,如果是可用的url则会创建对象成功,并且可以通过对象获取到此url中相关的信息,比如协议头,端口,ip地址等
package test;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.util.Patterns.GOOD_IRI_CHAR;
/**
 * Created by fuzhongbin on 2018/2/7.
 */

public class judgeUrl {
        private String mScheme;
        private String mHost;
        private int mPort;
        private String mPath;
        private String mAuthInfo;

        static final int MATCH_GROUP_SCHEME = 1;
        static final int MATCH_GROUP_AUTHORITY = 2;
        static final int MATCH_GROUP_HOST = 3;
        static final int MATCH_GROUP_PORT = 4;
        static final int MATCH_GROUP_PATH = 5;

        /* ENRICO: imported the ParseExeption here */
        public static class ParseException extends RuntimeException {
            public String response;

            ParseException(String response) {
                this.response = response;
            }
        }
        //根据正则表达式进行判断  
        static Pattern sAddressPattern = Pattern.compile(  
            /* scheme    */ "(?:(http|https|file)\\:\\/\\/)?" +  
            /* authority */ "(?:([-A-Za-z0-9$_.+!*'(),;?&=]+(?:\\:[-A-Za-z0-9$_.+!*'(),;?&=]+)?)@)?" +  
            /* host      */ "([" + GOOD_IRI_CHAR + "%_-][" + GOOD_IRI_CHAR + "%_\\.-]*|\\[[0-9a-fA-F:\\.]+\\])?" +  
            /* port      */ "(?:\\:([0-9]*))?" +  
            /* path      */ "(\\/?[^#]*)?" +  
            /* anchor    */ ".*", Pattern.CASE_INSENSITIVE);

        /** parses given uriString. */
//用法:将需要判断的url传入,new WebAddress(address) 如果抛出异常,则会出现问题  
        public judgeUrl(String address) throws ParseException {
            if (address == null) {
                throw new NullPointerException();
            }

            // android.util.Log.d(LOGTAG, "WebAddress: " + address);  

            mScheme = "";
            mHost = "";
            mPort = -1;
            mPath = "/";
            mAuthInfo = "";

            Matcher m = sAddressPattern.matcher(address);
            String t;
            if (m.matches()) {
                t = m.group(MATCH_GROUP_SCHEME);
                if (t != null) mScheme = t.toLowerCase(Locale.getDefault());
                t = m.group(MATCH_GROUP_AUTHORITY);
                if (t != null) mAuthInfo = t;
                t = m.group(MATCH_GROUP_HOST);
                if (t != null) mHost = t;
                t = m.group(MATCH_GROUP_PORT);
                if (t != null && t.length() > 0) {
                    // The ':' character is not returned by the regex.  
                    try {
                        mPort = Integer.parseInt(t);
                    } catch (NumberFormatException ex) {
                        throw new ParseException("Bad port");
                    }
                }
                t = m.group(MATCH_GROUP_PATH);
                if (t != null && t.length() > 0) {  
                /* handle busted myspace frontpage redirect with 
                   missing initial "/" */
                    if (t.charAt(0) == '/') {
                        mPath = t;
                    } else {
                        mPath = "/" + t;
                    }
                }

            } else {
                // nothing found... outa here  
                throw new ParseException("Bad address");
            }  
  
        /* Get port from scheme or scheme from port, if necessary and 
           possible */
            if (mPort == 443 && mScheme.equals("")) {
                mScheme = "https";
            } else if (mPort == -1) {
                if (mScheme.equals("https"))
                    mPort = 443;
                else
                    mPort = 80; // default  
            }
            if (mScheme.equals("")) mScheme = "http";
        }

        @Override
        public String toString() {
            String port = "";
            if ((mPort != 443 && mScheme.equals("https")) ||
                    (mPort != 80 && mScheme.equals("http"))) {
                port = ":" + Integer.toString(mPort);
            }
            String authInfo = "";
            if (mAuthInfo.length() > 0) {
                authInfo = mAuthInfo + "@";
            }

            return mScheme + "://" + authInfo + mHost + port + mPath;
        }

        public void setScheme(String scheme) {
            mScheme = scheme;
        }

        public String getScheme() {
            return mScheme;
        }

        public void setHost(String host) {
            mHost = host;
        }

        public String getHost() {
            return mHost;
        }

        public void setPort(int port) {
            mPort = port;
        }

        public int getPort() {
            return mPort;
        }

        public void setPath(String path) {
            mPath = path;
        }

        public String getPath() {
            return mPath;
        }

        public void setAuthInfo(String authInfo) {
            mAuthInfo = authInfo;
        }

        public String getAuthInfo() {
            return mAuthInfo;
        }
}
希望对大家有用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值