URL.openStream() 返回不为空,或者不抛异常就是可以连接

解决方案 »

  1.   

    URLConnection urlconnection = new URLConnection(URL***);IF urlconnection.connected == false
       NOT connected
    ELSE
       Connected
      

  2.   

    真的是非常不好意思,原来那个我也没有自己试一下,就是凭感觉写了。后来自己试了一下,发现问题是因为那个类的成员变量connected是protected的,所以不能直接引用,但是居然也没有一个方法可以get到。那就只好不用这个方法了,我这里写了一个,你看看对你的思路有没有帮助,主要就是那个方法testURLConnection(URL url)的用处了。有什么问题再跟我讲吧?
    import java.net.*;
    import java.io.*;class Test1
    {
      public static void main(String [] args)
      {
          try
          {
           int i = args.length;
           URL[] url = new URL[i];
           boolean testResult;
           for(int j=0; j<i; j++)
           {           url[j] = new URL(args[j]);
               testResult = testURLConnection(url[j]);
               System.out.println("The test result for " + url[j].toString() +" is "+ testResult);       }
          }
             catch(MalformedURLException e)
             {
                e.printStackTrace();
             }  }  public static boolean testURLConnection(URL url)
      {
        InputStream output;
        try
        {
          output = url.openStream();
          return true;
        }
        catch(IOException e)
        {
          return false;
        }
      }
    }