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