Hello!
I have been creating a program in Processing Java to get the HTML of a site with HTTP GET in the Networking Library. I followed the instructions in the Processing Network Tutorial, but it only shows how to get the HTML for one site, www.ucla.edu.
I initially started this topic because I got a 400 Bad request code, but now I realize the documentation of the network library is not great, and I don’t have a clue how to redirect to a different site.
The tutorial says I should use
Client c;
c = new Client(this, "www.ucla.edu", 80); // Connect to server on port 80
c.write("GET / HTTP/1.0\n"); // Use the HTTP "GET" command to ask for a webpage
c.write("Host: www.ucla.edu\n\n"); // Tell the server for which domain you are making the request
but I can’t use this to connect to other sites or pages.
Does anyone know the way to connect to other sites?
Thanks and sorry this is really badly worded…
Please share your attempts and your efforts.
I have found a couple of post from previous posts:
Below is an example using HTTP GET / and using loadString()
. The value of OUTPUT_LEN defines the amount of data to output in the console.
Kf
//REFERENCES: Text only websites https://sjmulder.nl/en/textonly.html
//===========================================================================
// IMPORTS:
import http.requests.*;
//===========================================================================
// FINAL FIELDS:
final int OUTPUT_LEN=150;
final String[] urls = {
"http://www.example.com/",
"http://www.google.com/",
"http://lite.cnn.io/en/"
};
//===========================================================================
// PROCESSING DEFAULT FUNCTIONS:
void settings() {
size(400, 600);
}
void setup() {
textAlign(CENTER, CENTER);
rectMode(CENTER);
fill(255);
strokeWeight(2);
for (String url : urls) {
getRequest(url);
getData(url);
delay(2000); //NOT really needed but always be a good citizen
}
exit();
}
void draw(){}
//===========================================================================
// OTHER FUNCTIONS:
void getRequest(String url) {
GetRequest get = new GetRequest(url);
get.send();
String ret=get.getContent();
println("====================GET /===========================");
println("URL: ", url, "returned", ret.length(), "bytes (?)");
println("Response Content:", ret.substring(0, OUTPUT_LEN), "...");
}
void getData(String url) {
String[] str = loadStrings(url);
String ret=join(str, '\n');
println("====================LOADSTRING===========================");
println("URL: ", url, "returned", ret.length(), "bytes (?)");
println("Response Content:", ret.substring(0, OUTPUT_LEN), "...");
}
1 Like
OK, thanks!! I’ll try that.