1

Java、GWT、およびAPIとの相互作用は初めてです。簡単な質問です。

次のcurlコマンドを使用してRESTAPIと正常に対話しました。

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

チュートリアルから、上記のcurlコマンドと同じように、リモートサーバーにリクエストを送信することを期待している次のコードがあります。

私が理解しようとしているのは(グーグルからの愛情なしに)、URLを送信するときにOPERATION_NAME、TECHNICIAN_KEY、およびINPUT_DATAパラメーターを渡す方法です。任意の提案、チュートリアルなどをいただければ幸いです。

以下は私のサーバー側の実装インターフェースからのものです:

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

    try {
        //dividing url into host: http://some.server
        //path: a/path/in/it
        //and parameters: this=that&those=others

        int hostStart= serviceUrl.indexOf("//");

        int pathStart= serviceUrl.substring(hostStart + 2).indexOf("/");

        int parameterStart= serviceUrl.substring(hostStart + 2 + pathStart).indexOf("?");

        final String serverHost= serviceUrl.substring(0, hostStart + pathStart + 2);

        final String serverPath= serviceUrl.substring(hostStart + 3, 
                hostStart + pathStart + 2 + parameterStart);

        final String serverParameters= serviceUrl.substring(hostStart + pathStart + 3 + parameterStart);

        final  URL url = new URL(serverHost);

        final URLConnection connection= url.openConnection();
        connection.setDoOutput(true);

        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");
        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) {
        throw new HelpDeskTestException();

    }
4

1 に答える 1

1

次のライブラリの使用を検討してください:ApacheHttpClient。これを使用してPOSTリクエストを作成する例を次に示します。

于 2012-10-04T23:45:23.350 に答える