GWT错误:“输出流已被提交并且不能再写入” [英] GWT Error: "The output stream has been committed and can no longer be written to"
问题描述
Java和GWT新手。这是我的问题。我收到此错误消息:OutputStream已被提交并且不能再被写入。而我试图通过REST API将xml发布到远程服务器。
它在以下代码的这一行中发生:
out.write(Content-Type:application / x-www-form-urlencoded \r\\\
);
这可从终端使用:
curl -dOPERATION_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><描述>再次测试curl输入< / description>< / Details>< / Operation> http://app.company.com/sdpapi/request/
我无法翻译上面的curl命令转化为下面的代码。我从教程中获得了以下代码,但我不确定如何正确传递网址和参数。任何建议或疑难解答的其他方法将不胜感激。我在google上找不到任何东西。
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 {
$ b $ @Override
public String getFromRemoteServer(String serviceUrl)
throws HelpDeskTestException {
字符串结果=;
尝试{
最终网址url =新网址(serviceUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine; ((inputLine = in.readLine())!= null){
result + = inputLine;
}
in.close();
返回结果;
} catch(final Exception e){
throw new HelpDeskTestException();
$ b}
@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 /请求者%3E%3Csubject%3ETest%3C /受试者%3E%3Cdescription%3ETesting%20GWT%20input%20again%3C /描述%3E%3C /细节%3E%3C /操作%3E; //将参数放在这里进行测试。
最终网址url =新网址(serverHost);
final URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setConnectTimeout(10000); / /添加这个看我是否可以解决超时问题。
connection.setReadTimeout(10000);
final OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
out.write(POST+ serverPath +\r\\\
);
out.write(Host:+ serverHost +\r\\\
);
out.write(Accept-Encoding:identity \r\\\
);
out.write(Connection:close \r\\\
);
out.write(Content-Type:application / x-www-form-urlencoded \r\\\
); (Content-Length:+ serverParameters.length()+\r\\\
\r\\\
+
serverParameters +\r\\\
);
String result =;
String inputLine; ((inputLine = in.readLine())!= null){
result + = inputLine;
}
in.close();
out.close();
返回结果;
catch(最终异常e){
System.out.println(e.getMessage());
抛出新的HelpDeskTestException();
}
}
}
我认为使用HttpURLConnection代替普通的URLConnection会更容易。它具有设置标题和其他属性的便利方法。请参阅此问题的接受答案,了解如何使用它来执行POST的一个很好的示例:
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();
}
}
}
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
这篇关于GWT错误:“输出流已被提交并且不能再写入”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!