URLs

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

  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
    #2
    1. Advertising

  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
    >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
    Roedy Green, Aug 2, 2005
    #3
  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
    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
    freesoft_2000, Aug 2, 2005
    #4
    1. Advertising

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
    Replies:
    3
    Views:
    474
    Corey Martin
    Apr 6, 2004
  2. Kaidi
    Replies:
    5
    Views:
    454
    Andrew Thompson
    Jan 4, 2004
  3. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    731
    Sriram Srivatsan
    Aug 12, 2008
  4. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Replies:
    11
    Views:
    6,118
    Adam Monsen
    Feb 8, 2009
  5. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    273
    Steve T.
    Mar 4, 2004
Loading...

Share This Page