I form a message to send via VkAPI, try to put transitions to another line and make a GET request via OkHttp, but the message comes glued.

What I want to get:

Website: websiteName

Description: Description

What I get:

Website: websiteName Description: description

How a GET request is sent:

String message = "https://api.vk.com/method/messages.send?user_id=91215491&v=5.52&message=" + "Сайт: " + websiteName + "\n" + "Описание: " + description + "&access_token=TOKEN"; client.setConnectTimeout(5, TimeUnit.SECONDS);//Connect timeout client.setReadTimeout(5, TimeUnit.SECONDS);//Socket timeout Request request = new Request.Builder().url(message).build(); Response response = client.newCall(request).execute(); 

I also tried via \ r \ n and just in case through String Builder, but the problem was with OkHttp, because for the sake of the same jsoup test for the sake of the request was sent normally and transfers came fine.

  • one
    Try replacing "\n" with "%0A" - mymedia
  • Thanks, it turned out! - Stas Mackarow

2 answers 2

The problem was the non-escape of special characters in a URI. Before transmitting the text of the message, encode it with percentages. Like this

 String messageText = "Сайт: " + websiteName + "\nОписание: " + description; String encodedMessage = URLEncoder.encode(messageText, "ISO-8859-1"); String requestUrl = "https://api.vk.com/method/messages.send?v=5.52&access_token=TOKEN&user_id=91215491&message=" + encodedMessage; ... 

Also consider using a variety of URI builders .

    It was necessary to replace "\ n" with "% 0A".

    • This option solves the problem only partially. In the substituted variables, generally speaking, there can be anything. It would be nice to encode them too. - mymedia