[11104] in www-talk@info.cern.ch
Re: POSTing with a Socket
daemon@ATHENA.MIT.EDU (Jacob W. Anderson)
Thu Feb 12 21:52:18 1998
From: "Jacob W. Anderson" <jwa@zen.praja.com>
To: <chris@surewould.com>, <www-talk@w3.org>
Date: Thu, 12 Feb 1998 18:23:43 -0800
Resent-From: www-talk@w3.org
> URL url = new URL(urlName);
> Socket webSock = new Socket(url.getHost(), 80);
If you're using a proxy, this strategy will fail. You can get the
output stream for the URL by calling URL.openConnection() and casting
the URLConnection class to an HttpURLConnection. From HttpURLConnection
you can get the output stream, which you can convert to a DataOutputStream
and voila'!
> out.println("POST " + url.getFile() + " HTTP/1.0");
> out.println("Connection: Keep-Alive");
> ...
You need "\r\n" (CRLF) as the terminator for each header line. When
you're done with the headers, just before the content, you need to send a
blank line CRLF:
POST this/url/thingie HTTP/1.1\r\n
Connection: Keep-Alive\r\n
Content-Type: application/octet-stream\r\n
Content-Length: 500\r\n
\r\n
500 bytes of data
That's all it takes. Since you are specifying Keep-Alive, you need to
request HTTP/1.1 as your protocol, otherwise you can't do keep-alive and
the server will cut off the connection when it generates a response.
--Jacob W. Anderson (jwa@praja.com)