如何使用loadStrings(),loadXML()或processing.net库在Processing中进行HTTP身份验证?

时间:2012-12-10 23:30:08

标签: java processing

我有一个链接:

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

我想从网站加载信息。我尝试过使用loadStrings(url),loadXML(url)(信息实际上是xml),没有骰子,我得到一个HTTP状态401.

创建HTTP客户端似乎是最有希望的,但我仍然无法让它工作。这是代码:

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

这是我得到的错误:

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)

2 个答案:

答案 0 :(得分:0)

我使用java.net库而不是processing.net库。我能够找到一些答案here。所以把它放到处理中,它看起来像这样:

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() {
}

答案 1 :(得分:0)

您只是没有正确设置HTTP标头。这是一个有效的例子,我甚至设置了一个受密码保护的测试文件:

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]);
    }
  }
}

对于您的用例,此代码可能有效,但如果没有正确的用户名和密码,我无法对其进行测试:

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]);
    }
  }
}