HttpUrlConnection or URL to retrieve information
Im working on an application to retrieve information from a website.
The website is www.kingsofchaos.com, a game.
The following code doesn't work, but when I change the url to google, yahoo, or any other website, it works perfectly...
import java.net.*;
import java.io.*;
publicclass URLReader{
publicstaticvoid main(String[] args)throws Exception{
URL koc =new URL("http://www.kingsofchaos.com/");
BufferedReader in =new BufferedReader(
new InputStreamReader(
koc.openStream()));
String inputLine;
while ((inputLine = in.readLine()) !=null)
System.out.println(inputLine);
in.close();
}
}
[1259 byte] By [
tylerrubya] at [2007-11-26 12:52:01]

# 3
You can set the headers as,
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR
2.0.50727)"); // Will set IE user agent
# 5
this code works for me:
URL koc = new URL("http://www.kingsofchaos.com/");
URLConnection connection = koc.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR
2.0.50727)"); // Will set IE user agent
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();