域名的匹配,即测试两个网站的url是不是层次匹配的,



public class DNSStringMatching {
public static boolean DNSStrMatch(String dns,String url){
boolean flag = false ;

String tmpdns = dns.trim();//去空格
String tmpstr="";
String tmpurl = url.trim();
if(tmpdns.length() > tmpurl.length()) return flag;
if(tmpdns.length()>=7){
tmpstr = tmpdns.substring(0, 7);
if(tmpstr.equals("http://")){
tmpdns = tmpdns.substring(7);//截取http://后面的内容
}
}

if(tmpdns.length()>=4){
tmpstr = tmpdns.substring(0, 4);
if(tmpstr.equals("www.")){
tmpdns = tmpdns.substring(4);//截取www.后面的内容
}
}
if(tmpurl.length()>=7){
tmpstr = tmpurl.substring(0, 7);
if(tmpstr.equals("http://")){
tmpurl = tmpurl.substring(7);//截取http://后面的内容
}
}

if(tmpurl.length()>=4){
tmpstr = tmpurl.substring(0, 4);
if(tmpstr.equals("www.")){
tmpurl = tmpurl.substring(4);//截取www.后面的内容
}
}


int pos = tmpurl.indexOf(tmpdns);//tmpdns在url中的起始位置

String ss = "";
if(pos > 0) ss = tmpurl.substring( 0 , pos);
int ishx = ss.indexOf("/");//判断是否是伪造的
int strlen = tmpdns.length();//tmpdns的长度
int dotposd = tmpdns.indexOf(".");
int dotposu = tmpurl.indexOf(".");
if(tmpdns.substring(strlen-1).equals("/"))//tmpdns最后一个字符是'/',则只要url中存在tmpdns则肯定能匹配
{
if(-1 != pos){
if(-1 != ishx)//存在"/",则说明是伪造的
return false ;
else{
if(!tmpdns.substring(0, dotposd).equals(tmpurl.substring(0, dotposu))){
while(0 <= (pos-1)){
if(tmpurl.substring(pos - 1, pos).equals("."))
return true ;
else return false ;
}

}
else{
flag = true;
}
}
}
}
else
{

String tag="";
if(-1 != pos){
if(-1 != ishx)//存在"/",则说明是伪造的
return false ;
else{
int ulen = tmpurl.length() - pos;
if(ulen == tmpdns.length()){//dns与url右边的子串匹配
if(!tmpdns.substring(0, dotposd).equals(tmpurl.substring(0, dotposu))){
while(0 <= (pos-1)){
if(tmpurl.substring(pos - 1, pos).equals("."))
return true ;
else return false ;
}

}else return true ;

}else{//不是和右边的子串匹配
tag = tmpurl.substring(pos + strlen, pos + strlen +1);
}
}
}
if(tag.equals("/")){ 
if(!tmpdns.substring(0, dotposd).equals(tmpurl.substring(0, dotposu))){
while(0 <= (pos-1)){
if(tmpurl.substring(pos - 1, pos).equals("."))
return true ;
else return false ;
}


}else return true ;
}
}
return flag;
}
public static void main(String[] args){
String dns[]={"http://www.people.com.cn/","http://xinhuanet.com","www.hb.xinhuanet.com/","www.hb.xinhuanet.com","www.b.xinhuanet.com/","www.b.xinhuanet.com","www.xinhuanet.com/","xinhuanet.com/2013-08/14","http://www.hnb.n.xinhuanet.com.cn","www.b.xinhuanet.com/","xinhuanet.com/2013-08/14/c_116945022.htm","http://xinhuanet.com/ ","b.xinhuanet.com/","http://xinhuanet.com/ ","tv.cn","xinhuanet.com","www.xinhuanet.com","http://www.xinhuanet.com","xinhuanet.org"};//这些是测试用例
String url="http://fujian.people.com.cn/n/2013/0705/c181466-19006919.html";
for(String str:dns){
if(DNSStringMatching.DNSStrMatch(str, url))
System.out.println("DNS:"+str+"  URL:"+ url + "  Match success!");
else
System.out.println("DNS:"+str + "  URL:"+url+ "  Match failed!");
}
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值