簡體   English   中英

Java程序嘗試將字符串發送到Node.js應用程序Rest API鏈接

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

我有一個正在偵聽傳入請求的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");
});     

您需要對字符串進行URL編碼。 參見http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM