java socket编程检查域是否为真错误
java socket programming check domain is true or not error
我正在从文件中读取域并检查域格式是否正确。如果域正确,则将其写入 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.2\executor-snippets\run.xml:53: Java returned: 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();
}
}
我正在从文件中读取域并检查域格式是否正确。如果域正确,则将其写入 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.2\executor-snippets\run.xml:53: Java returned: 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();
}
}