HTTP Request in Processing

This program shows how to make a HTTP request in Processing.

Technorati Tags: ,


/* http client
 by Tom Igoe
 
 Starts a network client that connects to a server on port 80,
 sends an HTTP 1.1 GET request, and prints the results.
 
 created March 18, 2005
 
 */
import processing.net.*;

Client client;
int byteCount = 0;

void setup()
{
  size(200, 200);
  noStroke();
  // open a TCP socket to the host:
  client = new Client(this, "myserver.com", 80);

  //print the IP address of the host:
  println(client.ip());

  // send the HTTP GET request:
  client.write("GET /~myaccount/somePage.html HTTP/1.1\n");
  client.write("HOST: myserver.com\n\n");
}

void draw()
{
  background(0);
  // print the results of the GET:
  if (client.available() > 0) {
    int inByte = client.read();
    byteCount++;
    print((char)inByte);
  } 
  else {
    if (byteCount > 0) {
      println(" I got: " + byteCount + " bytes.");
      byteCount = 0;
      println("\n\nThat's all, folks!\n");
    }
  }
}