简体   繁体   中英

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

I have a nodejs app that is listening for incoming request. I make a call to that rest link using a Java program. It is trying to send data to the NodeJS app. It works if the string is one word. However If my string is long enough or contains special character it would fail with error.

What would be my best choice to use in order to send string that may contains special character and size of atleast 100 - 200 characters or even more ?

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 Function to send data to the 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 listening for incoming data from the Java app)

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM