0

I have a link:

String url = "http://username:password@url.com"

And I want to load the information from the site. I've tried using loadStrings(url), loadXML(url) (info is actually in xml), and no dice, I get a HTTP Status 401.

Creating an HTTP Client seemed the most promising, but I'm still unable to get it to work. This is the code:

import processing.net.*;
Client c;
String data;

void setup() {
  size(600, 600);
  c = new Client(this, "http://username:password@my.idigi.com/ws/DataPoint/dia/channel/00000000-00000000-00409DFF-FF521E03/XBee_4079C12D/spikes_back", 80);
  c.write("GET / HTTP/1.0\r\n");      
}

void draw() {
    if (c.available() > 0) {
    data = c.readString();
    println(data);
  }
}

This is the error I get:

java.net.UnknownHostException: http://username:password@my.idigi.com/ws/DataPoint/dia/channel/00000000-00000000-00409DFF-FF521E03/XBee_4079C12D/spikes_back
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)
    at processing.net.Client.<init>(Unknown Source)
    at getSoundData.setup(getSoundData.java:28)
    at processing.core.PApplet.handleDraw(PApplet.java:2117)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193)
    at processing.core.PApplet.run(PApplet.java:2020)
    at java.lang.Thread.run(Thread.java:680)
java.lang.NullPointerException
    at processing.net.Client.write(Unknown Source)
    at processing.net.Client.write(Unknown Source)
    at getSoundData.setup(getSoundData.java:29)
    at processing.core.PApplet.handleDraw(PApplet.java:2117)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193)
    at processing.core.PApplet.run(PApplet.java:2020)
    at java.lang.Thread.run(Thread.java:680)
John Saunders
  • 160,644
  • 26
  • 247
  • 397
luisdaniel
  • 907
  • 11
  • 20

2 Answers2

0

Instead of the processing.net library, I used the java.net library. I was able to find some answers here. So putting it into processing, it looked something like this:

import java.net.*;
String baseURL = "http://my.idigi.com/ws/DataPoint/dia/channel/00000000-00000000-00409DFF-FF521E03/XBee_4079C12D/spikes_back";
URL url;
String userPassword = "user"+":"+"password";

void setup() {
  size(600, 600);
  try {
    String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
    url = new URL(baseURL);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + encoding);
    conn.connect();
    println(conn.getResponseCode());
    println(conn.getContent());
  } 
  catch (Exception e) {
    println("exception" + e);
  }
}

void draw() {
}
Community
  • 1
  • 1
luisdaniel
  • 907
  • 11
  • 20
0

You're just not setting your HTTP header properly. Here is an example that works, I even set up a password protected testfile:

import processing.net.*; 
import javax.xml.bind.DatatypeConverter; //for Base64 encoding
Client c; 
String dataIn;
String[] headerData;

String host = "fla.sc";
String file = "/testfile";

//The username and password need to be set in Base64
String auth = DatatypeConverter.printBase64Binary("user:password".getBytes());

void setup(){
  size(200, 200);
  c = new Client(this, host, 80);
  c.write("GET " + file + " HTTP/1.1\n");
  c.write("Authorization:  basic  " + auth + "\n");
  c.write("Host: " + host + "\n"); // the host to connect to
  c.write("\n"); // close the header
}

void draw() { 
  if (c.available() > 0) {
    dataIn = c.readString();
    println(dataIn);
    headerData = split(dataIn, '\n');
    for(int i=0; i<headerData.length; i++){
      print(i);
      print(": ");
      println(headerData[i]);
    }
  }
}

For your use case, this code might work, but I can't test it without your proper username and password:

import processing.net.*; 
import javax.xml.bind.DatatypeConverter; //for Base64 encoding
Client c; 
String dataIn;
String[] headerData;

String host = "my.idigi.com";
String file = "/ws/DataPoint/dia/channel/00000000-00000000-00409DFF-FF521E03/XBee_4079C12D/spikes_back";

//The username and password need to be set in Base64
String auth = DatatypeConverter.printBase64Binary("username:password".getBytes());

void setup(){
  size(200, 200);
  c = new Client(this, host, 80);
  c.write("GET " + file + " HTTP/1.1\n");
  c.write("Authorization:  basic  " + auth + "\n");
  c.write("Host: " + host + "\n"); // the host to connect to
  c.write("\n"); // close the header
}

void draw() { 
  if (c.available() > 0) {
    dataIn = c.readString();
    println(dataIn);
    headerData = split(dataIn, '\n');
    for(int i=0; i<headerData.length; i++){
      print(i);
      print(": ");
      println(headerData[i]);
    }
  }
}
spex
  • 1,110
  • 10
  • 21