validate from a Java program: how to send CSS and receive results

I try to incorporate a validation in a Java program of mine: a CSS file is
read and sent over the net to the validator. I then expect results back.
Below is a piece of the code.
I use examples from SUN, concerning sending/receiving  over an
URLConnection.

 

I get an error 500 back from the server, along with the error message:

 

Servlet has thrown exception:java.lang.IllegalStateException: Reader used

 

What do I do wrong and how can I correct it ?

 

 

import java.io.*;

import java.net.*;

import java.lang.Thread.*;

 

public class CSSValidator {

    private URL url;

    private URLConnection connection;

    

    public CSSValidator () {

        try {

            url = new URL("http://jigsaw.w3.org/css-validator/validator");

            connection = url.openConnection();

            connection.setDoOutput(true);

        }

        catch (MalformedURLException me) {

            System.err.println("MalformedURLException: " + me);

        }

        catch (IOException ioe) {

            System.err.println("IOException: " + ioe);

        }

    }

 

    public String validateFile( String fn ) throws IOException {

        String line;

        StringBuffer response = new StringBuffer() ;

        try {

            BufferedReader b = new BufferedReader(new FileReader(fn )) ;

 

            PrintWriter outStream = new
PrintWriter(connection.getOutputStream());

 

            outStream.print("text=");

            while ((line = b.readLine()) != null) {

                  line = URLEncoder.encode(line,"UTF-8");

                  response.append(line + "\n");

                  outStream.print(line);

            }

            outStream.close();

            Thread.sleep(2000);

        }

        catch (IOException ioe) {

            System.out.println(ioe.getMessage());

        }

        catch (InterruptedException ie) {

            System.out.println(ie.getMessage());

        }

        System.out.println(response.toString());

        response.delete(0, response.length());

 

       // DataInputStream inStream = new
DataInputStream(connection.getInputStream()

        BufferedReader inStream;

        try {

            

            inStream = new BufferedReader(new
InputStreamReader(connection.getInputStream()));  // ç Here the IOException
occurs

            System.out.println("wait for response:");

 

        }

        catch (IOException ioe) {

            if (! (connection instanceof HttpURLConnection))

                    throw ioe;

            InputStream err = ((HttpURLConnection) connection).
getErrorStream();

            if (err == null)

                throw ioe;

            inStream = new BufferedReader (new InputStreamReader(err));

            System.out.println(ioe.getMessage());

        }

        

        while ((line = inStream.readLine()) != null) {

             response.append(line + "\n");

        }

        inStream.close();

        return response.toString();

    }

}

Received on Tuesday, 5 January 2010 10:57:07 UTC