Dopo aver importato
codice:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
Il codice a posto è così:
codice:
String siteLink = "http://www.unimi.it/paginachenonesiste.html";
try
{
URL url = new URL(siteLink);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestProperty("User-Agent", "userAgent");
c.connect();
int responseCode = c.getResponseCode();
if (responseCode == 404)
{
Fa quello che deve fare
}
}
catch (MalformedURLException mue)
{
mue.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
Se faccio andare col debug il response che mi torna è 200 non 404. Su una tabella che ho visto il codice 200 dice che è tutto corretto quando ovviamente non è vero perchè la pagina non esistetabella