View Single Post
Old 03-06-2005, 05:44 PM   #1 (permalink)
wishknew
Wisnu Widiarta
 
wishknew's Avatar
 
Join Date: Feb 2005
Location: Indonesia
Posts: 14
wishknew is on a distinguished road
Send a message via Yahoo to wishknew
Error java.net.ProtocolException: Cannot write output after reading input.

Hello guys,

I've got stuck with making a post connection using https.
Here is my code:

HTTPS Client
Code:
public void run() {
        HttpsURLConnection httpsConnection = null;
        int responseCode=0;
        String address = "https://" + config.getHTTPSServerAddress();
        SSLContext sc = null;
        String encodedContent;

        prepareSecureConnection(sc);

        httpsConnection = openConnection(address, httpsConnection, responseCode);
        printResponseContent(httpsConnection);
        httpsConnection.disconnect();

        encodedContent = getEncodedContent();
        writeEncodedContent(httpsConnection, encodedContent);//error here
    }

    private void writeEncodedContent(HttpsURLConnection httpsConnection, String encodedContent) {
        OutputStreamWriter writer;
        try {
             outputStream = httpsConnection.getOutputStream();
             writer = new OutputStreamWriter(outputStream);
             writer.write(encodedContent);
             writer.flush();
             outputStream.close();

        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getEncodedContent() {
        String content = "";
        int data;
        try {
            inputStream = clientIFX.getInputStream();
            data = inputStream.read();
            while (data != -1) {
                content += (char)data;
                data = inputStream.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (content.equals("")) {
            return "";
        }
        else {
            return Base64.encodeBytes(content.getBytes(), Base64.GZIP);
        }
    }

    private void printResponseContent(HttpsURLConnection connection) {
        StringBuffer buffer;
        InputStream input;
        BufferedReader dataInput;
        String line;
        String responseContent = "";
        try {
            buffer = new StringBuffer();
            input = connection.getInputStream();
            dataInput = new BufferedReader(new InputStreamReader(input));
            while ((line = dataInput.readLine()) != null) {
                buffer.append(line);
                buffer.append('\n');
            }
            input.close();
            responseContent = (String) buffer.toString().trim();
        } catch (Exception e) {
            System.err.println(e);
        }
        System.out.println(responseContent);

    }

    private HttpsURLConnection openConnection(String address, HttpsURLConnection connection, int responseCode) {
        URL url;
        try {
            url = new URL(address);
            connection = (HttpsURLConnection) url.openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);

            responseCode = connection.getResponseCode();

        } catch (Exception e) {
            System.err.println("Ooops : " + e + " (Response code is " + responseCode + ")");
        }
        return connection;
    }

    private void prepareSecureConnection(SSLContext sc) {
        try {
            sc = SSLContext.getInstance("SSLv3");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        TrustManager[] tma = {this};
        try {
            sc.init(null, tma, null);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        SSLSocketFactory ssf = sc.getSocketFactory();
        HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
The error was:
java.net.ProtocolException: Cannot write output after reading input.
at sun.net.www.protocol.http.HttpURLConnection.getOut putStream(HttpURLConnection.java:565)
at sun.net.www.protocol.https.HttpsURLConnectionImpl. getOutputStream(DashoA12275)
at IFXCompressor.writeEncodedContent(IFXCompressor.ja va:55)
at IFXCompressor.run(IFXCompressor.java:49)


My question is how to post a message using HttpsURLConnection? (without doing any get at all)

Thank you,


WishKnew
wishknew is offline   Reply With Quote