URLs

F

freesoft_2000

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);

FileInputStream out = new FileInputStream("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
 
T

The alchemist

richard, first of all first try whether you can read the file in such a
way, i mean only reading the file from url connection and not posting.
I am sure, well nearly sure, that you will not be able to.
You see, you are skipping in this process all the TCP/IP norms and
security measures.
You will not be able to read anything like this from any website,
unless it is a webservice and allows you too.

I think you better do the first half of your work, to be sure whether
it is possible even.
 
R

Roedy Green

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 don't quite follow you, but authenticators are amazingly automatic.
You set them up ahead of time, and they automatically turn on and do
their thing only if needed. Your mainline logic carries on as if
there were no authentication involved.

Here is some authenticator logic from the Replicator that gets invoked
if the website requires a password to download files.


/**
* Provides Receiver userid/password as needed to the website.
*
* @author Roedy Green
* @version 1.0
* @since 2003-10-05
*/
public class ReceiverAuthenticator extends Authenticator {

/**
* Called when password authorization is needed. Subclasses should
override
* the default implementation, which returns null.
*
* @return The PasswordAuthentication collected from the user, or
null if
* none is provided.
*/
protected PasswordAuthentication getPasswordAuthentication ()
{
return new PasswordAuthentication( ConfigForReceiver.USERID,
ConfigForReceiver.PASSWORD.toCharArray() );
}
}


to register your authenticator class :

Authenticator.setDefault( new ReceiverAuthenticator() );

That's it. No other code need.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
F

freesoft_2000

Hi everyone,

alchemist the TCP/IP and security measures are already
taken care by the URL class.
So to answer you question yes i am able to connect to any website if it
exists.

Roedy i understand your code completely but from what you
are saying that if i am trying to post something to the website by getting
its outputStream(which will most certinly reaquire authentication) all i
have to do is set the below line

Authenticator.setDefault(Some authenticator class)
And any authentication will be taken care of.
Am i understanding you correctly Roedy??

As for my second question i am afraid that the server commands may get
sved to gether with the zip file which i try to download from the website
(see my code on the second part on my first post)
Do any one of you guys think that could possibly happen??

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top