JavaとGWTの新機能。これが私の問題です。「OutputStreamがコミットされ、書き込みできなくなりました」というエラーメッセージが表示されます。RESTAPIを介してリモートサーバーに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();
}
}
}