Base64 Encode string
in
Programming Questions
•
10 months ago
I can't seem to find a best practice for Base64 encoding a string in Processing.
I want to do this in order to properly pass the
HTTP Authorization header via the
Processing Network library.
The now defunct Mobile Processing had a great function for this called
setAuthorization, but of course this is no longer available.
Any thoughts on best method for Base64 encoding a string in Processing?
Here is my current working test code, with line 12 being a manual Base64 encode:
(the actual username and password to the testfile really are just user:password, you can run this code)
- import processing.net.*;
- Client c;
- String dataIn;
- String[] headerData;
- String host = "fla.sc";
- String file = "/testfile";
- //This is the string "user:password" encoded as Base64
- String auth = "dXNlcjpwYXNzd29yZA==";
- 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]);
- }
- }
- }
1