0

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();

    }


}

}

4

1 に答える 1

1

プレーンなURLConnectionの代わりにHttpURLConnectionを使用する方が簡単だと思います。ヘッダーやその他のプロパティを設定するための便利なメソッドがあります。POSTを実行するためにそれを使用する方法の良い例については、この質問に対する受け入れられた回答を参照してください。

Java-POSTメソッドを介してHTTPパラメータを簡単に送信

于 2012-10-09T15:25:51.517 に答える