Java 和 GWT 的新手。这是我的问题。我收到此错误消息:“OutputStream 已提交,无法再写入。” 当我试图通过 REST API 将 xml 发布到远程服务器时。
它发生在下面代码的这一行:
out.write("Content-Type: application/x-www-form-urlencoded\r\n");
这适用于终端:
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/
我无法将上面的 curl 命令转换为下面的代码。我从教程中获得了以下代码,但我不确定如何正确传递 URL 和参数。任何建议或其他故障排除方法将不胜感激。我在谷歌上没有找到很多东西。
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();
}
}
}