java套接字编程检查域名是否为真的错误

问题描述 投票:0回答:1

我从文件中读取域名,并检查域名格式是否正确。如果域名正确,就把它写到true.txt,如果不正确,就把它写到wrong.txt。我的问题是,程序从文件中读取一些域名后,会抛出一个异常。我把我的代码和错误附在下面。

/********************************************************************************************/


  BufferedReader br = null;
  FileWriter fw=null;
  FileWriter ft=null;

    String strLine = "";

    try {
        br = new BufferedReader( new FileReader("test.txt"));
        fw= new  FileWriter("wrongfile.txt");
            ft= new  FileWriter("truefile.txt");


        while( (strLine = br.readLine()) != null){
            System.out.println(strLine);
            URL u = new URL ("http://"+strLine);
   HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
   huc.setRequestMethod ("GET");  
   huc.setRequestMethod ("HEAD"); 
   huc.connect();

   int coded = huc.getResponseCode() ;
            System.out.println("code:"+coded);
  int  code=coded/100;
  if(code==3||code==4||code==5)
    {
        fw.write(strLine+":error:"+coded+"\n");
         System.out.println("domain is not ok:"+coded);
    }
   else
  {
       ft.write(strLine+"\n");
       System.out.println("domain is ok:");

   }

        }

    } catch (FileNotFoundException e) {
        System.err.println("File not found");

    } 
     br.close();
         fw.close();
         ft.close();
 }

这里是错误部分

           Exception in thread "main" java.net.UnknownHostException: bushido.academy
           at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
           at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
           at java.net.Socket.connect(Socket.java:589)
           at java.net.Socket.connect(Socket.java:538)
           at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
     at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
   at sun.net.www.http.HttpClient.New(HttpClient.java:308)
   at sun.net.www.http.HttpClient.New(HttpClient.java:326)
   at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
   at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
   at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
   at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
    at checker.Urlchecker.main(Urlchecker.java:50)
  C:\Users\Imran\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
java sockets https network-connection
1个回答
-1
投票
   public static void main(String[] args) throws IOException {
   int coded = 0;
   int counter=0;
   int num=10;

      Scanner in = new Scanner(System.in);
    System.out.print("Enter the file name:");
    String filename=in.nextLine();

  BufferedReader br = null;
  FileWriter fw=null;
  FileWriter ft=null;
  String f="truefile.txt";
    String strLine = "";

        br = new BufferedReader( new FileReader(filename));
        fw= new  FileWriter("wrongfile.txt");

            ft= new  FileWriter(f);


        while( (strLine = br.readLine()) != null){


    try {
            System.out.println(strLine);

            URL u = new URL ("http://"+strLine);
    HttpURLConnection huc =  ( HttpURLConnection )  
    u.openConnection () ; 





    //huc.setRequestMethod ("GET");  
       huc.setRequestMethod ("HEAD");

    coded = huc.getResponseCode() ;

  int  code=coded/100;

  if(code==4 || code==5 || coded==303)
    {
        fw.write(strLine+":error:"+coded+"\n"+"\n");
         System.out.println("domain is not ok error:"+coded);
    }

   else
  {
       ft.write(strLine+"\n"+"\n");
       System.out.println("domain is ok:");

   }
    counter++;
            System.out.println("total domain checked:"+counter);

            } 

    catch (FileNotFoundException e) {
        System.err.println("File not found");

    }
    catch (IOException e) {
        System.out.println("domain not exist fatal error");


    }

   catch(InterruptedException e)
   {
       System.out.println("timeout");
   }


        }
       br.close();
       ft.close();
       fw.close();



 }



}
© www.soinside.com 2019 - 2024. All rights reserved.