W3C home > Mailing lists > Public > www-talk@w3.org > January to February 1998

POSTing with a Socket

From: Chris <chris@surewould.com>
Date: Thu, 12 Feb 1998 16:53:34 -0500
Message-ID: <34E36F5E.3B21@surewould.com>
To: www-talk@w3.org
I'm trying to write some Java code that sends data to an HTML form.
(Technically, I want it to send some encoded data to the URL specified
in the form's ACTION attribute, but you know what I mean). The form I
want to send to uses POST, and I can't seem to figure out how to send
the POST data so it gets to the server. I've written a fake server so I
can spy on what Netscape sends when it POSTs form data, but it still
doesn't work. It seems that I can get the URL just fine, but I can't
post any data.

I've included the source below. Can anyone help?

Thanks,
Chris


    static public void post (String urlName, String fileName, String
postData)
    {
        try
        {
        // Open output file
        PrintWriter outfile = new PrintWriter(new
FileOutputStream(fileName));

        // Connect to the web server & get streams
        URL url = new URL(urlName);
        Socket webSock = new Socket(url.getHost(), 80);
        PrintWriter out = new PrintWriter(new
DataOutputStream(webSock.getOutputStream()));
        DataInputStream in = new
DataInputStream(webSock.getInputStream());

        // Send header stuff to the server (I stole this from what
Netscape sends)
        out.println("POST " + url.getFile() + " HTTP/1.0");
        out.println("Connection: Keep-Alive");
        out.println("User-Agent: Mozilla/3.04 (Win95; I)");
        out.println("Host: chrisl.radix.net");
        out.println("Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, */*");

        // Send the POST data
        out.println("Content length: 134");                // I
hardwired the value just for now
        out.println("");
        out.println(postData);
        out.println("");
        out.flush();

        // Eat the returned page & spit it out
        String line;
        while ((line = in.readLine()) != null)
        {
                outfile.println(line);
                outfile.flush();
        }

        // Close everything
        in.close();
        out.close();
        outfile.close();

        }   catch (Exception ex)
            {
                System.err.println(ex.getMessage());
                ex.printStackTrace(System.err);
            }
    }
Received on Thursday, 12 February 1998 16:52:31 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 27 October 2010 18:14:23 GMT