We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › same-server http requests
Page Index Toggle Pages: 1
same-server http requests (Read 494 times)
same-server http requests
Dec 14th, 2008, 9:12am
 
I'd appreciate some help.  I'm trying to demonstrate that an applet can print out the html of the page it's imbedded in, to demonstrate that applets can get their own location and request that location.  I can't get it to work at all; I keep getting an UnknownHostException, even though the host is the very site that serves up the applet.  Please try it yourself and see the code at:
http://myernore.com/dev/processing/URL_test/index.html
As far as I've read, an applet is supposed to be able to "phone-home" to its server to get more information without having to be signed.  The jars I've put on my server ARE signed, however ( ALL of them - core.jar, net.jar, URL_test.jar ).  As you can see from the printout, it has no (real) trouble acquiring its own location.  It fails to request the address, though.  Any clues?
Re: same-server http requests
Reply #1 - Dec 14th, 2008, 5:19pm
 
I think your request wasn't correct (content of HTML headers).
Here is something that isn't rejected by the server...
Code:
  url = "http://myernore.com/dev/processing/URL_test/index.html";
try{
if( url != null )
{
println( "Hello from document base " + url );
String[] m = match(url, "^http://([^/]+)(/.*$)");
c = new Client(this, m[1], 80); // Connect to server on port 80
println(m[1] + " " + m[2]);
c.write("GET " + m[2] + " HTTP/1.1\r\n"); // Use the HTTP "GET" command to ask for a Web page
c.write("Host: " + m[1] + "\r\n");
c.write("Accept: text/html,application/xhtml+xml,application/xml\r\n");
}
}
catch (NullPointerException e)
{
e.printStackTrace();
}

You also need to init data (new StringBuilder()).
Note: I haven't got the page, but I hope I push you in the right direction...
Re: same-server http requests
Reply #2 - Dec 15th, 2008, 1:45am
 
Hi PhiLho,
Thanks for your suggestions.  
I implemented them two different (and equally unsuccessful) ways 1.) once as an adaptation of my code: http://myernore.com/dev/processing/URL_test_5/index.html 2.) once as an adaptation of your code: http://myernore.com/dev/processing/URL_test_6/index.html.  The main difference between the two is that the url is dynamically coded in the first example, and the url is statically coded in the second example.  For some reason, there are no bytes available, that is, the server doesn't send anything else.  I can't figure out why.  Any additional hints?
Re: same-server http requests
Reply #3 - Dec 15th, 2008, 4:12am
 
Hi,
I finally got it to work.  See [http://www.myernore.com/dev/processing/URL_test_7/] for the result.  

The main thing I was getting wrong: I duplicated the host name in the GET request, and included too many newlines in the GET request.  I still don't know how to request the root from my server; I see in the examples that you can request the root of processing.org's server by doing simply "GET / HTTP/1.1\n" - in other words, the / stands for the root as it does in unix systems.  On my host, however, / returns a 404, even though I have it defined ...

Thanks for your help ...
////////////////////////////////////////////
/**
* HTTP Client.
*
* Starts a network client that connects to a server on port 80,
* sends an HTTP 1.1 GET request, and prints the results.
*/


import processing.net.*;
import java.lang.StringBuilder;
import java.applet.Applet;

Client c;
StringBuilder data;
PFont font;
String host, file;
PVector start, change;

void setup() {
 start = new PVector(0,0);
 change = new PVector(0,0);
 Applet thisApplet = (Applet)this;
 host = thisApplet.getDocumentBase().getHost();
 file = thisApplet.getDocumentBase().getFile();
 size(600, 600);
 background(50);
 fill(200);
 textFont( createFont( "arial", 18 ) );
 data = new StringBuilder();
 c = new Client(this, host, 80); // Connect to server on port 80
 c.write("GET " + file + " HTTP/1.1\n"); // Use the HTTP "GET" command to ask for a Web page
 c.write("Host: " + host + "\n\n"); // Be polite and say who we are
}

void draw() {
 translate( change.x, change.y );
 cursor(MOVE);
 background( 50 );
 fill( 200 );
 if (c.available() > 0) { // If there's incoming data from the client...
   data.append( c.readString() ); // ...then grab it and print it
 }
 text( data.toString(), 10, 10 );
}

void mouseDragged()
{
 change.x += mouseX - pmouseX;
 change.y += mouseY - pmouseY;  
}
Page Index Toggle Pages: 1