Use the class below like:
Code:
String urlStr="http://www.thesite.com/";
URL url=new URL(urlStr);
SiteConn sC=new SiteConn(url);
StringBuffer sb=sC.getContents();
if (sb.toString.equals("And now...some services")) {
// well well...
}
(note: it returns the entire html code for the url)
SiteConn class:
Code:
public class SiteConn {
URL url=null;
public SiteConn(URL url) {
this.url=url;
}
public StringBuffer getContents() throws Exception {
StringBuffer buffer;
String line;
int responseCode;
HttpURLConnection connection;
BufferedReader dataInput;
connection = (HttpURLConnection)url.openConnection();
responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("HTTP response code: " +
String.valueOf(responseCode));
}
try {
buffer = new StringBuffer();
InputStream input = connection.getInputStream();
dataInput = new BufferedReader(new InputStreamReader(input));
while ( (line = dataInput.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
//System.out.println("line: " + line);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace(System.err);
return null;
}
return buffer;
}
}
Bookmarks