0

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

Ross
  • 97
  • 6
  • 1
    have you tried urlencoding the apiToken and chatId and text? https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters – Gandalf Nov 08 '19 at 08:10
  • 1
    What is array ` Array[i]`? – ieggel Nov 08 '19 at 08:33
  • @ieggel String array, used for example – Ross Nov 08 '19 at 08:36
  • 1
    @gandalf, please show us the value of Array[i]. I suspect the error lies there. – ieggel Nov 08 '19 at 08:37
  • @ieggel String[] Array= time.split("\\d\n");First String String1 String2 44:2 something like this in array[i] – Ross Nov 08 '19 at 08:45
  • @gandalf, please show more code. also the time var. – ieggel Nov 08 '19 at 08:49
  • @ieggel Elements elemenx = doc.select("div.elements, span.team"); String time = elemenx.html(); String[] Array = time.split("\\d\n"); – Ross Nov 08 '19 at 08:53
  • Possible duplicate of [Java URL encoding of query string parameters](https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters) – SJN Nov 08 '19 at 09:26
  • `String apiToken = "123843242734723";` these should be hidden from above code for security reasons. – Vishwa Ratna Nov 08 '19 at 10:08

2 Answers2

1

It seems that the content in Array[i] comes from a html input element. I suspect there is some kind of whitespace such as \r\n that is passed to the URL, which then causes the MalformedURLException.

Here my approach:

    public static void main(String[] args) throws IOException {
        // Here is where you would assign the content of your HTML element
        // I just put a string there that might resemble what you get from your HTML
        String timeHtmlInput = "12:00\r\n13:00\r\n14:00\r\n";

        // Split by carriage return
        String timeTokens[] = timeHtmlInput.split("\r\n");

        String urlString = "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s";
        String apiToken = "123843242734723";
        String chatId = "@Example";
        String time = timeTokens[0];
        String text = time + "Hello";

        urlString = String.format(urlString, 
                URLEncoder.encode(apiToken, "UTF-8"), 
                URLEncoder.encode(chatId, "UTF-8"),
                URLEncoder.encode(text, "UTF-8"));

        URL url = new URL(urlString);
        System.out.println(url);
        URLConnection conn = url.openConnection();
    }

BTW it is always good practice to encode the query string parameters, such as:

URLEncoder.encode(text, "UTF-8"));

as they also might contain some other illegal characters. Hope this helps!

ieggel
  • 891
  • 6
  • 12
  • It Works!!! Thanks for help. I would not find the solution by myself. Have a nice day, you are a very kind person – Ross Nov 08 '19 at 10:31
  • @Ross you're welcome :). Keep on coding, you will improve faster than you think. – ieggel Nov 08 '19 at 10:35
0

The @ character must be encoded. E.g. replace it directly with %40. but you can also use

URLEncoder.encode(s,"UTF-8")
Laertes
  • 334
  • 1
  • 6
  • that is not the reason he is getting `java.net.MalformedURLException`. – ieggel Nov 08 '19 at 08:40
  • the `encode` method should be used because there might me more invalid characters in the whole url. – f1sh Nov 08 '19 at 08:41
  • Encoding the whole URL String will not work. Only the query strings should be encoded. Otherwise the proocol it will result in `java.net.MalformedURLException: no protocol`. – ieggel Nov 08 '19 at 10:08
  • @ieggel this is a good additional advice. My answer was more of a general hint how to encode a generic string. – Laertes Nov 08 '19 at 10:27