Java Sockets

URL Usage

try {
  //Open it with a url
  URL u = new URL("http://jmvidal.cse.sc.edu");

  //or with separate parts
  URL u2 = new URL("http", "jmvidal.cse.sc.edu", "/");

  InputStream in = URL.openStream(); //strips headers
  int c;
  while ((c = in.read()) ~= -1) {
    write(c);
  }

  URLConnection uc = url.openConnection(); //keeps headers
  InputStream in2 = uc.getInputStream();

  //Get content and try to convert it it apporpiate object
  // using the provided MIME information.
  Object o = url.getContent();

  //Eliminates bad characters, like spaces
  URLEncoded.encode("A b"); //A+b

  //Simple way to fetch a document:
  URLClient client = new URLClient();
  String yahoo = client.getDocumentAt("http://www.yahoo.com");
  System.out.println(yahoo);

}  catch (MalformedURL Exception e){
}  catch (IOException e) e) {
} 


José M. Vidal .

2 of 9