【问题标题】:Java program trying to send string to a nodejs app Rest API linkJava 程序尝试将字符串发送到 nodejs 应用程序 Rest API 链接
【发布时间】:2015-03-14 12:25:13
【问题描述】:

我有一个正在侦听传入请求的 nodejs 应用程序。我使用 Java 程序调用了那个 rest 链接。它正在尝试将数据发送到 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链接

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 链接监听来自 Java 应用的传入数据)

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

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

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

【问题讨论】:

    标签: java node.js rest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多