Hi everyone,

I have two rather silly questions about URLs but please bear with me for a while.

Usually when you use the URLConnection class to read from a website you don't get prompted for authentication but if you are trying to post something to that website you will usually require some kind of authentication.

Please assume that the authenticator class has been set correctly and that its getPasswordAuthentication() method returns the correct username and passsword

Consider this code

Authenticator.setDefault(an authenticator class)

URL url1 = new URL("http://www.yahoo.com/my_life.txt");
URLConnection urlc = url1.openConnection();
urlc.setOutput(true);

//The below command line is where my concern is.
//If i am trying to post something to that website 
//will i be prompted by the Authenticator class 
//which i set above at the beginning of this code snippet
 
PrintWriter out = new PrintWriter(urlc.getOutputStream());

out.println("I love Java");

out.close

//other codes

My problem is now i do not know that if i am going to post to that website(and it most certainly will need authentication) will i be prompted by the Authenticator class so i can get the outputstream and post to that website

I am not able to test this code because i currently do not have any website to post to but i am working on an application that could possibly do this.

On another question lets say i am reading from a website that has a zip file say www.yahoo.com/my_life_story.zip

Now if i do this am i right??

URL url1 = new URL("http://www.yahoo.com/my_life_story.zip");
URLConnection urlc = url1.openConnection();
urlc.setOutput(false);

FileOutputStream out = new FileOutputStream("C:/my_life_story.zip");
InputStream in = urlc.getInputStream();

//other codes to read that file to disk

Now in above code i am reading stream bytes but if i were using the PrintReader and putting the output to the console you would see all the server html commands and the byte content of the file but as rubbish text.

Now basically my question is when i read that file as stream bytes and save it to disk will all the html commands also be saved because that's not what i want. I only want the zip file.

I hope someone can help me with the above two questions

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

This is what I've always used to download files. I've downloaded quite a few mpegs with it and it worked just fine.

public class HTTPStuff
{
public static Downloader(String fetchUrl, String saveAs)
{
  try
  {
    URL url = new URL(fetchUrl);
    InputStream in = url.openStream();
    FileOutputStream out = new FileOutputStream(saveAs);
    bye[] buff = new byte[4*1024]; //4k buffer
    int bytesRead;
    while((bytesRead=in.read(buf))!=-1)
    {
        out.write(buf,0,bytesRead);
    }
    in.close();
    out.close();
  }
    catch(MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
   {
        e.printStackTrace();
   }
}

I'm not sure what your question is on the whole authenticator issue...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.