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 › Network Library - Client write
Page Index Toggle Pages: 1
Network Library - Client write (Read 1069 times)
Network Library - Client write
Mar 10th, 2009, 11:36pm
 

Can anyone shed some light on what's going on with the Client library in this case:

I connect my client:

String req = "www.geoplugin.net";
String getReq = "/xml.gp?ip=12.215.42.19";

void setup() {
...
 c = new Client(this, req, 80); // Connect to server on port 80
}

then in the draw try once to do the following:

   c.write("GET "+getReq+" \n");
   c.write("Host: www.geoplugin.net\n");
   c.write("User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7\n");
   c.write("Accept: text/html,application/xhtml+xml,application/xml\n");
   c.write("Accept-Language: en-us,en;q=0.5\n");
   c.write("Accept-Encoding: gzip,deflate\n");
   c.write("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n");
   c.write("Keep-Alive: 300\n");
   c.write("Connection: keep-alive\n");

This works fine in Telnet, in a little Python script I wrote up, i.e. I get:

HTTP/1.1 200 OK
Date: Tue, 10 Mar 2009 22:21:34 GMT
Server: Apache
Content-Length: 696
...

etc

but in my Processing app I always get back

<h2>Error 404</h2>

I think it's mangling something, but I can't figure out what's going on. Is this a weird Java string class problem? Any ideas would be sweet. Thanks!
Re: Network Library - Client write
Reply #1 - Mar 11th, 2009, 12:01am
 
Wierd...just as a heads up to people, you have to do:

c.write("GET http://www.geoplugin.net/xml.gp?ip=12.215.42.19 \n");

for the request. If anyone feels like explaining why that is, lemme know Smiley
Re: Network Library - Client write
Reply #2 - Mar 11th, 2009, 1:33pm
 
It's because you're badly forming the request (and using the wrong line endings), your first line should actually be:
c.write("Get "+getReq+" HTTP/1.1\r\n");

Without the HTTP/1.1 definition the Host: field is ignored, since it'll default to HTTP/1.0.. or possibly even 0.9 hence having to put it in the first line for your workaround.
Re: Network Library - Client write
Reply #3 - Mar 11th, 2009, 3:09pm
 
Uh, what JohnG said (I was about to make a similar answer).

I will add that the last write should send a double \r\n to mark the end of the request.
Just tried and got a nice XML file about Sugar Grove.

I suppose some servers has some tolerance, or perhaps Python fix the line endings on the fly, etc., but it is better to get things right... Smiley
Re: Network Library - Client write
Reply #4 - Mar 11th, 2009, 3:13pm
 
Wow, yeah I hadn't even noticed that I had accidentally dropped the HTTP/1.1 from the initial request. It was in there, and I know why it should be, but I think a long day had my head a little fuzzy. The "Get" versus "GET" is another one that I actually didn't know about, is that 1.0 vs 1.1? Or is it irrelevant?
Re: Network Library - Client write
Reply #5 - Mar 11th, 2009, 3:57pm
 
Oops, it should be GET not Get.. that was my mistake.
Page Index Toggle Pages: 1