测试URL有效性

//测试URL有效性
public static  void testWebserviceConnection() throws Exception  {
//1  ,       2, 3, 4, 5, 6, 7, 8, 9, 10 ,11
String t="http://tg, http://172.17.16.100:8180/admin, http://172.17.16.100:8280/suite,http://tyty, http://172.17.16.100:8380/suite, http://172.17.16.124:8280/suite,  http://4545rtrt, http://172.17.16.160:8280/suite, http://ghhg, http://uyjh,http://31qa.sz.ecv/QA/ccp";
String t2="http://172.17.16.115:8080/suite, http://172.17.16.100:8080/b2b1, http://172.17.16.98:8080/suite, http://172.17.16.126:8080/suite, http://172.17.16.71:8080/suite, http://172.17.16.89:8080/b2b, http://172.17.16.122:8080/suite, http://172.17.16.133:8280/b2b";
String t3="http://788888jh, http://565656, http://172.17.16.100/suite, http://172.17.16.94:8180/refapp, http://172.17.16.115:8080/refapp, http://172.17.16.113:8080/refapp, http://172.17.16.113:8080/suite, http://172.17.16.94:8080/suite, http://172.17.16.100:8280/b2b1, http://172.17.16.133:8080/suite";
String[] s=t.split(",");
/**
* 1 http://255.255.255.255[/]
* 2 http://255.255.255.255:1234[/]
* 3 http://*.[com]|[cn]|[org]|[net]
* 3 255.255.255.255[/]
* 4 255.255.255.255:1234[/]
* 5 http
*/

int count=1;

URL url =null;
HttpURLConnection conn=null;
long start=0;
long start2=System.currentTimeMillis(); 
for(String u:s){
try{  
url = new URL(u);  
start=System.currentTimeMillis();
conn= (HttpURLConnection)url.openConnection();  
       conn.setConnectTimeout(500);//set the connect time out,it needs <10
       System.out.println("测试:连接URL一次需要:"+(System.currentTimeMillis()-start)+"毫秒");
       conn.setReadTimeout(1000);//set the read time out,it need more .The normal is set 1 second. when it throws UnknownHostException,it will need much time .
       int state = conn.getResponseCode();  
       if (state==200 || state==301 || state==302){
        System.out.println("完成:请求URL一次需要:"+(System.currentTimeMillis()-start)+"毫秒"+"=========="+"成功:"+count);
       }else{
        System.out.println("完成:请求URL一次需要:"+(System.currentTimeMillis()-start)+"毫秒"+"=========="+"失败:"+count);
       }  
       count++;
      }catch(SocketTimeoutException e){  //连接超时1.java.net.SocketTimeoutException: connect timed out  2.java.net.UnknownHostException: 4545rtrt(不明原因)
      System.out.println("异常(连接超时):失败请求URL一次需要:"+(System.currentTimeMillis()-start)+"毫秒"+"======"+"失败:"+count);
      count++;
      }catch (Exception e) {
      System.out.println("不明原因:失败请求URL一次需要:"+(System.currentTimeMillis()-start)+"毫秒"+"======"+"失败:"+count);
      count++;

}
System.out.println("总共:"+(System.currentTimeMillis()-start2)+"毫秒");

}


下一篇:用线程控制访问请求URL超时问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值