package basics.internet;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import basics.unexpected.Problem;
import basics.utl.SysUtl;

public class WGet
{
public String readUrl(String urlString, boolean linefeed) throws Problem
{
   URL url = null;
   URLConnection connection = null;
   BufferedReader oBReader = null;
   StringBuilder sb = new StringBuilder();
   try
   {
      url = new URL(urlString);
      // open the connection and prepare it to POST
      connection = url.openConnection();
      // logger.debug(urlString);
      connection.setDoOutput(false);
      connection.setDoInput(true);
      connection.setAllowUserInteraction(false);
      // Read the response
      oBReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      String sOutput = null;
      while((sOutput = oBReader.readLine()) != null)
      {
         sb.append(sOutput);
         if(linefeed)
         {
            sb.append("\n");
         }
      }
      oBReader.close();
   }
   catch(Exception e)
   {
      throw new Problem("Not found: {" + url.toString() + "}");
   }
   return sb.toString();
}

public static void main(String [] args)
{
   try
   {
      SysUtl.trace(new WGet().readUrl(args[0], true));
   }
   catch(Problem e)
   {
      SysUtl.terror(null, args[0] + " -- ?");
   }
}
}
