Package java.net

Cours électricité la résistance équivalente, tutoriel & guide de travaux pratiques en pdf.

vérifier des URL
import java.io.*;
import java.net.*;
public class VerifieURL {
public static void verifie(URLConnection u)
throws IOException
{if (!u.getContentType().equals(« text/html »))
{System.out.println(
« seuls les text/html sont verifies. »); System.exit(0);};
DataInputStream dis =new
DataInputStream(u.getInputStream());
String s=dis.readLine();
String motif1= »<A HREF= »;
String motif2= »> »;
String ref;
int i,j;
while (s!=null)
{i=s.indexOf(motif1);
if (i!=-1)
{j=s.indexOf(motif2); ref=s.substring(i+motif1.length()+1,j-1); try
{ValiditeURLConnexion.test(ref);} catch (IOException e)
{System.out.println(« Wrong URL : « +ref);}
}
s=dis.readLine();}
dis.close();
}
vérifier des URL (suite)
public static void main(String args[]) throws MalformedURLException, IOException
{if (args.length!=1)
{System.out.println(« Usage : java verifieURL « ); System.exit(0);}
URLConnection c=null;
try
{URL url =new URL(args[0]); ValiditeURLConnexion.test(url); c= url.openConnection();}
catch (IOException e)
{System.out.println(« Wrong URL : « +args[0]);
System.exit(0);}
verifie(c);
}
}
classe ValiditeURLConnexion
import java.io.*;
import java.net.*;
public class ValiditeURLConnexion {
public static void test(URL url) throws IOException {URLConnection c=url.openConnection(); DataInputStream dis =new
DataInputStream(c.getInputStream());
dis.close();
}
public static void test(String s) throws MalformedURLException,IOException
{test(new URL(s));
}

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *