Here's 2 ways to do it.
Thor HW
///////////////////////////////////// old style
////////////////////////////////////

    try {

      String userpass = new String
(_userDomain+"/"+_username+":"+_password);
      URL url = new URL (_AuthDomainURL);

      System.out.println ("authenticating the url with "+
_userDomain+"/"+_username);
      String encoding = Base64Encoder.encode (userpass.getBytes());

      URLConnection uc = url.openConnection();
      uc.setRequestProperty ("Authorization", "Basic " + encoding);
      InputStream content = (InputStream)uc.getInputStream();
      BufferedReader in =
        new BufferedReader (new InputStreamReader (content));
      String line;
      while ((line = in.readLine()) != null) {
        System.out.println (line);
      }

    } catch (MalformedURLException e) {
      System.out.println ("Invalid URL");
      return false;

    } catch (IOException e) {
      System.out.println ("Error reading URL");
      return false;
    }
///////////////////////////////////// new style
////////////////////////////////////
    // Install Authenticator
    Authenticator.setDefault (new MyAuthenticator ());
    try {
      URL url = new URL (urlString);
      InputStream content = (InputStream)url.getContent();
      BufferedReader in =
        new BufferedReader (new InputStreamReader (content));
      String line;
      while ((line = in.readLine()) != null) {
        System.out.println (line);
      }
    } catch (MalformedURLException e) {
      System.out.println ("Invalid URL");
    } catch (IOException e) {
      System.out.println ("Error reading URL");
    }


    class MyAuthenticator extends Authenticator {
      private String user_name   = new String();
      private String user_passwd = new String();

      public void setUsername (String username) {
        this.user_name = username;
      }

      public void setPassword (String password) {
        this.user_passwd = password;
      }

      protected PasswordAuthentication getPasswordAuthentication () {
        return new PasswordAuthentication (this.user_name, (char
[])this.user_passwd.toCharArray());
      }
    }



----- Original Message -----
From: "Andy Mayer" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, July 26, 2000 1:26 AM
Subject: Re: FileNotFoundError in servlet


> Whilst we are taking about URL's I thought I would sneak in this question
> which has been bugging me lately in a servlet....
>
> How do you modify the code below to pass a password and username in cases
> where the URL is protected?
>
> I can't find any examples in the documentation....
>
> Thanks,
>
> Andy
>
> ----- Original Message -----
> From: Gokul Singh <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Wednesday, July 26, 2000 6:03 AM
> Subject: Re: FileNotFoundError in servlet
>
>
> > Hi,
> >
> > You can not use a URL in the constructor of FileInputStream. For opening
a
> > connection you will have to use the URL object.
> > Try this code in place of your code.
> >
> > /* FileInputStream fistr = new
> >  FileInputStream("hostname:8080/examples/servlets/filename");
> >  DataInputStream in = new DataInputStream(fistr);*/
> >
> > URL source = new URL("hostname:8080/examples/servlets/filename");
> > DataInputStream in  = new DataInputStream( source.openStream() );
> >
> > I hope this works.
> >
> > Regds,
> > Gokul
> >
> >
> > ----- Original Message -----
> > From: "Syed Mahmoods" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Wednesday, July 26, 2000 3:32 AM
> > Subject: FileNotFoundError in servlet
> >
> >
> > > I have just installed the jsdk2.1.  So all the file structures are
> > standard
> > > to jsdk2.1, and I am new to servlets.
> > > My servlet is in the /home/username/jsdk2.1/examples/WEB-INF/servlets/
> > > directory.  I am trying to read a text file  and I get the
IOException:
> > > java.io.FileNotFoundException:
hostname:8080/examples/servlets/filename.
> > > I need to read this input file and then post the data to a cgi script.
> > > How do I specify the path so I am able to read the file?
> > >
> > > Here is part of the code:
> > >
> > > String newLine = System.getProperty("line.separator");
> > > FileInputStream fistr = new
> > > FileInputStream("hostname:8080/examples/servlets/filename");
> > > DataInputStream in = new DataInputStream(fistr);
> > > String aLine;
> > >  while ((aLine = in.readLine()) != null)
> > >  {
> > >    postString=postString+newLine+aLine;
> > >  }
> > >
> > > Thanks for all the help.
> > >
> > >
> >
>
___________________________________________________________________________
> > > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> > body
> > > of the message "signoff SERVLET-INTEREST".
> > >
> > > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > > Resources:
http://java.sun.com/products/servlet/external-resources.html
> > > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> > >
> >
> >
>
___________________________________________________________________________
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > Resources: http://java.sun.com/products/servlet/external-resources.html
> > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to