I want to send post request to some service using Java. Requests must contain some parameters and one of them is multiline String. I tried many ways but everytime I end up with whole text submitted as single line without newline characters. Any ideas?
URL url = new URL("http://example.com/create");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
String data = "param1=value&string=" + convertNewLineCharacters(text);
writer.write(data);
writer.flush();
// (...)
private String convertNewLineCharacters(String text) throws Exception {
String encodedString = URLEncoder.encode(text, "UTF-8");
encodedString = encodedString.replaceAll("%0A", "\r\n");
return encodedString;
}
I am limited to standard Java Api, so no external libraries are allowed.