I’m sending a message to telegram channel and i have error
Simple string sent, but modified by the type of part of the array is not sent
String urlString = "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s";
String apiToken = "123843242734723";
String chatId = "@Example";
String text = Array[i]+ " hello";
urlString = String.format(urlString, apiToken, chatId, text);
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection conn = url.openConnection();
Exception in thread “main” java.net.MalformedURLException: Illegal character in URL
2
Answers
The @ character must be encoded. E.g. replace it directly with %40.
but you can also use
It seems that the content in Array[i] comes from a html input element. I suspect there is some kind of whitespace such as
rn
that is passed to the URL, which then causes theMalformedURLException
.Here my approach:
BTW it is always good practice to encode the query string parameters, such as:
as they also might contain some other illegal characters.
Hope this helps!