Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > URLs

Reply
 
 
freesoft_2000
Guest
Posts: n/a
 
      08-01-2005
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


 
Reply With Quote
 
 
 
 
The alchemist
Guest
Posts: n/a
 
      08-02-2005
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.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-02-2005
On Mon, 01 Aug 2005 19:51:09 -0400, "freesoft_2000"
<(E-Mail Removed)> wrote or quoted :

>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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
freesoft_2000
Guest
Posts: n/a
 
      08-02-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JDBC URLs ...not really URLs? Adam Monsen Java 11 02-08-2009 08:14 PM
Converting Relative URLs into Absolute URLs Nathan Sokalski ASP .Net 1 08-12-2008 07:03 AM
dynamic URLS convert to static URLS for search engines Steve T. ASP .Net Web Services 7 03-04-2004 03:16 PM
URLs not creating new tab Terry O'Brien Firefox 3 02-26-2004 04:49 AM
Distinguish text URLs from non-text URLs? Kaidi Java 5 01-04-2004 10:15 AM



Advertisments