GWT Error: “The output stream has been committed a

2019-09-04 17:33发布

问题:

New to Java and GWT. Here is my problem. I am getting this error message: "The OutputStream has been committed and can no longer be written to." while I am trying to post xml to a remote server via REST API.

It is happening on this line in the code below:

out.write("Content-Type: application/x-www-form-urlencoded\r\n");

This works from terminal:

curl -d "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=DxxxxxxxxxxxxxxxxxxxB6&INPUT_DATA=<?xml version=%221.0%22 encoding=%22utf-8%22?><Operation><Details><requester>Me</requester><subject>Test</subject><description>Testing curl input again</description></Details></Operation>" http://app.company.com/sdpapi/request/ 

I'm having trouble translating the above curl command into the code below. I got the following code from a tutorial and I'm not sure how to pass in the URL and parameters properly. Any suggestions or additional methods of troubleshooting will be appreciated. I did not find much of anything on google for this.

package com.gwt.HelpDeskTest.server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.gwt.HelpDeskTest.client.HelpDeskTestService;
import com.gwt.HelpDeskTest.shared.HelpDeskTestException;

@SuppressWarnings("serial")
public class HelpDeskTestImpl extends RemoteServiceServlet implements
    HelpDeskTestService {

@Override
public String getFromRemoteServer(String serviceUrl)
        throws HelpDeskTestException {


    String result = "";

    try {
        final URL url = new URL(serviceUrl);

        final BufferedReader in= new BufferedReader(new InputStreamReader(url.openStream()));

        String inputLine;
        while ((inputLine= in.readLine()) != null) {

            result+= inputLine;
        }

        in.close();
        return result;

    } catch (final Exception e) {
        throw new HelpDeskTestException();

    }

}

@Override
public String postToRemoteServer(String serviceUrl)
        throws HelpDeskTestException {

    try {




        final String serverHost= "http://app.company.com/";
        final String serverPath= "http://app.company.com/sdpapi/request/";


        final String serverParameters= 
                "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=Dxxxxxxxxxx6&INPUT_DATA=%3C?xml%20version=%25221.0%2522%20encoding=%2522utf-8%2522?%3E%3COperation%3E%3CDetails%3E%3Crequester%3EMe%3C/requester%3E%3Csubject%3ETest%3C/subject%3E%3Cdescription%3ETesting%20GWT%20input%20again%3C/description%3E%3C/Details%3E%3C/Operation%3E"; //put parameters here for testing.

        final  URL url = new URL(serverHost);

        final URLConnection connection= url.openConnection();
        connection.setDoOutput(true);
        connection.setConnectTimeout(10000); //added this to see if I can address the timeout issue.
        connection.setReadTimeout(10000);

        final OutputStreamWriter out= new OutputStreamWriter(connection.getOutputStream());

        final BufferedReader in= new BufferedReader(new InputStreamReader(
                connection.getInputStream()));

        out.write("POST " + serverPath + "\r\n");
        out.write("Host: " + serverHost + "\r\n");
        out.write("Accept-Encoding: identity\r\n");
        out.write("Connection: close\r\n");
        out.write("Content-Type: application/x-www-form-urlencoded\r\n"); //This    is where the error is occuring
        out.write("Content-Length: " + serverParameters.length() + "\r\n\r\n" +
            serverParameters + "\r\n");


        String result = "";
        String inputLine;

        while ((inputLine=in.readLine()) != null) {
            result+= inputLine;
        }

        in.close();
        out.close();

        return result;

    }  catch (final Exception e) {
        System.out.println(e.getMessage());
        throw new HelpDeskTestException();

    }


}

}

回答1:

I think it would be easier for you to use an HttpURLConnection instead of a plain URLConnection. It has convenience methods to set the headers and other properties. See the accepted answer to this question for a good example of how to use it to do a POST:

Java - sending HTTP parameters via POST method easily



标签: java rest gwt