英文:
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 = "";
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();
}
here is 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
答案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("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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论