Java套接字编程检查域名是否正确错误。

huangapple go评论74阅读模式
英文:

java socket programming check domain is true or not error

问题

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

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();

Error portion:

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
英文:

I'm reading domains from a file and check whether the domain format is correct or not. if domain is correct write it to true.txt and if not, write it to wrong.txt. My problem is that the program after reading some domains from file throws an exception. I've attached my code and errors below:

/********************************************************************************************/
BufferedReader br = null;
FileWriter fw=null;
FileWriter ft=null;
String strLine = &quot;&quot;;
try {
br = new BufferedReader( new FileReader(&quot;test.txt&quot;));
fw= new  FileWriter(&quot;wrongfile.txt&quot;);
ft= new  FileWriter(&quot;truefile.txt&quot;);
while( (strLine = br.readLine()) != null){
System.out.println(strLine);
URL u = new URL (&quot;http://&quot;+strLine);
HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
huc.setRequestMethod (&quot;GET&quot;);  
huc.setRequestMethod (&quot;HEAD&quot;); 
huc.connect();
int coded = huc.getResponseCode() ;
System.out.println(&quot;code:&quot;+coded);
int  code=coded/100;
if(code==3||code==4||code==5)
{
fw.write(strLine+&quot;:error:&quot;+coded+&quot;\n&quot;);
System.out.println(&quot;domain is not ok:&quot;+coded);
}
else
{
ft.write(strLine+&quot;\n&quot;);
System.out.println(&quot;domain is ok:&quot;);
}
}
} catch (FileNotFoundException e) {
System.err.println(&quot;File not found&quot;);
} 
br.close();
fw.close();
ft.close();
}

here is error portion:

           Exception in thread &quot;main&quot; 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.&lt;init&gt;(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

答案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("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();
}
英文:
   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(&quot;Enter the file name:&quot;);
String filename=in.nextLine();
BufferedReader br = null;
FileWriter fw=null;
FileWriter ft=null;
String f=&quot;truefile.txt&quot;;
String strLine = &quot;&quot;;
br = new BufferedReader( new FileReader(filename));
fw= new  FileWriter(&quot;wrongfile.txt&quot;);
ft= new  FileWriter(f);
while( (strLine = br.readLine()) != null){
try {
System.out.println(strLine);
URL u = new URL (&quot;http://&quot;+strLine);
HttpURLConnection huc =  ( HttpURLConnection )  
u.openConnection () ; 
//huc.setRequestMethod (&quot;GET&quot;);  
huc.setRequestMethod (&quot;HEAD&quot;);
coded = huc.getResponseCode() ;
int  code=coded/100;
if(code==4 || code==5 || coded==303)
{
fw.write(strLine+&quot;:error:&quot;+coded+&quot;\n&quot;+&quot;\n&quot;);
System.out.println(&quot;domain is not ok error:&quot;+coded);
}
else
{
ft.write(strLine+&quot;\n&quot;+&quot;\n&quot;);
System.out.println(&quot;domain is ok:&quot;);
}
counter++;
System.out.println(&quot;total domain checked:&quot;+counter);
} 
catch (FileNotFoundException e) {
System.err.println(&quot;File not found&quot;);
}
catch (IOException e) {
System.out.println(&quot;domain not exist fatal error&quot;);
}
catch(InterruptedException e)
{
System.out.println(&quot;timeout&quot;);
}
}
br.close();
ft.close();
fw.close();
}
}

huangapple
  • 本文由 发表于 2020年5月19日 18:33:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61888897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定