Discussion in 'Java' started by freesoft_2000, Aug 2, 2005.

  1. Hi everyone,

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

    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

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

    Consider this code

    Authenticator.setDefault(an authenticator class)

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

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


    //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

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

    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

    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
    freesoft_2000, Aug 2, 2005
    1. Advertisements

  2. 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.
    The alchemist, Aug 2, 2005
    1. Advertisements

  3. freesoft_2000

    Roedy Green Guest

    On Mon, 01 Aug 2005 19:51:09 -0400, "freesoft_2000"
    <> 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

    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
    * 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.

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Aug 2, 2005
  4. 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

    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
    freesoft_2000, Aug 2, 2005
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Corey Martin

    Making URLS/E-Mails Into Links

    Corey Martin, Apr 5, 2004, in forum: Perl
    Corey Martin
    Apr 6, 2004
  2. Jiong Feng
    Jiong Feng
    Nov 19, 2003
  3. MattB

    More on variable URLs

    MattB, Jan 9, 2004, in forum: ASP .Net
    Jan 9, 2004
  4. Showjumper

    How to create vanity urls?

    Showjumper, Jan 24, 2004, in forum: ASP .Net
    Jan 25, 2004
  5. Kaidi
    Andrew Thompson
    Jan 4, 2004
  6. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Sriram Srivatsan
    Aug 12, 2008
  7. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Adam Monsen
    Feb 8, 2009
  8. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Steve T.
    Mar 4, 2004