We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Reading urls protected with http authentication
Page Index Toggle Pages: 1
Reading urls protected with http authentication (Read 1523 times)
Reading urls protected with http authentication
Jun 5th, 2009, 3:07am
 
I want to read and use data from "Google Insights for Search", but the password authentication mechanism makes it impossible for me.

I found a solution for java : Quote:
If the access to the URL is protected with authentication mechanism, IOException is thrown when you try to read from InputStream associated with URLConnection. In that case, you need to use Authenticator class from java.net package.
The example code is shown below.


Code:

import java.net.*;
import java.io.*;

public class URLConnectionAuthReader {
   public static void main(String[] args) throws Exception {
       String username = "username";
       String password = "password";
       URL authurl = new URL(url);

       Authenticator.setDefault(new MyAuthenticator(username, password));
       URLConnection ac = authurl.openConnection();
       BufferedReader in = new BufferedReader(
                               new InputStreamReader(
                               ac.getInputStream()));
       String inputLine;

       while ((inputLine = in.readLine()) != null)
           System.out.println(inputLine);
       in.close();
   }

   protected static class MyAuthenticator extends Authenticator {
       private String username, password;

       public MyAuthenticator(String user, String pwd) {
           username = user;
           password = pwd;
       }

       protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(username, password.toCharArray());
       }
   }
}



But I don´t know how to use it in processing or if there exists something equal for processing.

Has anybody an idea?

Thanks, Silke
Re: Reading urls protected with http authentication
Reply #1 - Jun 5th, 2009, 8:32am
 
I think you will want to add the contents of main() to your setup() method. Then add the MyAuthenticator class to your sketch.

Don't forget to import the libraries.
Re: Reading urls protected with http authentication
Reply #2 - Jun 6th, 2009, 9:58am
 
The Authenticator code should work, but what about the special URL format:

 http://username:password@example.com/path/

If that works it might be simpler.  I'd test it myself, but can't think of a sample site to load.
Re: Reading urls protected with http authentication
Reply #3 - Jun 6th, 2009, 11:36am
 
Well, I have a Web server installed on my computer for test, so I followed the instructions at Apache's Authentication, Authorization, and Access Control and indeed I had to enter a password before entering the protected sub-folder.
The http://username:password@example.com/path/ scheme worked on the browser (except IE6...).
But alas, it doesn't work in Java (if used in a loadString()): I have a 401 error (Authentication Required, so scheme isn't understood, it might be a browser-only stuff).

I adapted the given code to test it and it worked fine:
Code:


/*
String[] s = loadStrings("http://localhost/Web/HTML.txt");
println(s[0]);
*/
void setup()
{
String username = "user";
String password = "pwd";
URL authurl = null;
try
{
authurl = new URL("http://localhost/Web/HTML.txt");
}
catch (MalformedURLException e) {}
Authenticator.setDefault(new MyAuthenticator(username, password));
BufferedReader in = null;
ArrayList readLines = new ArrayList();
String inputLine;
try
{
URLConnection ac = authurl.openConnection();
in = new BufferedReader(
new InputStreamReader(ac.getInputStream()));
while ((inputLine = in.readLine()) != null)
{
readLines.add(inputLine);
}
}
catch (IOException e) {}
finally
{
if (in != null) try { in.close(); } catch (IOException e) {}
}
String[] s = new String[readLines.size()];
readLines.toArray(s);
println(s[0]);
}

protected static class MyAuthenticator extends Authenticator
{
private String username, password;

public MyAuthenticator(String user, String pwd)
{
username = user;
password = pwd;
}

protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password.toCharArray());
}
}

Page Index Toggle Pages: 1