Java 试图将字符串发送到 nodejs 应用程序的程序 Rest API link

Java program trying to send string to a nodejs app Rest API link

我有一个正在侦听传入请求的 nodejs 应用程序。我使用 Java 程序调用其余 link。它正在尝试将数据发送到 NodeJS 应用程序。如果字符串是一个单词,它就可以工作。但是,如果我的字符串足够长或包含特殊字符,它将因错误而失败。

要发送可能包含特殊字符且大小至少为 100 - 200 个字符甚至更多的字符串,我的最佳选择是什么?

java.net.MalformedURLException: Illegal character in URL
        at sun.net.www.http.HttpClient.getURLFile(HttpClient.java:583)
        at sun.net.www.protocol.http.HttpURLConnection.getRequestURI(HttpURLConnection.java:2298)
        at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:513)

Java向Rest发送数据的函数link

public void sendDataToNodeJSApp(String create="Creating service APP in org test@ca.capge.com / space Testing as test@ca.capge.com..."){

        URL url;

        try {
            // get URL content

            String a="http://xyz/data/"+create;
            url = new URL(a);
            URLConnection conn = url.openConnection();

            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(
                               new InputStreamReader(conn.getInputStream()));

            String inputLine;
            while ((inputLine = br.readLine()) != null) {
                    System.out.println(inputLine);
            }
            br.close();

            System.out.println("Done");

        } 

    }

NodeJS(Rest Api link 侦听来自 Java 应用程序的传入数据)

app.get('/data/:STMT_TEXT', function (req, res) {

var STMT_TEXT = req.params.STMT_TEXT;
console.log(STMT_TEXT);

      res.send("Done");
});     

您需要URL 对字符串进行编码。参见 http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html