Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package util; import com.google.gson.JsonObject; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; /** * * @author u0181181 */ public class Slack { public Slack() { } public void sendMsgExecucaoIniciada(String titulo, String texto) { try { String str = "{ " + " \"attachments\": [ " + " { " + " \"fallback\": \"Monitoria DA\", " + " \"pretext\": \"Monitoria DA\", " + " \"title\": \"" + titulo + "\", " + " \"text\": \"" + texto + "\", " + " \"color\": \"#7CD197\" " + " } " + " ] " + "}"; System.out.println(str); sendMsg(new StringEntity(converteCaracteresEspeciais(str))); } catch (IOException ex) { new Msg().msgErro(ex.getMessage()); } } private static void sendMsg(StringEntity jsonMsg) throws IOException { String url1 = "https://hooks.slack.com/services/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"; String url2 = "https://hooks.slack.com/services/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"; sendMsg(jsonMsg, url1); sendMsg(jsonMsg, url2); } private static void sendMsg(StringEntity jsonMsg, String url) throws IOException { HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead try { HttpPost request = new HttpPost(url); request.addHeader("content-type", "application/json; charset=UTF-8"); request.setEntity(jsonMsg); HttpResponse response = httpClient.execute(request); System.out.println(response); } finally { httpClient.getConnectionManager().shutdown(); //Deprecated } } private static String converteCaracteresEspeciais(String str) { return str.replace("", "c").replace("", "C").replace("", "a"); } }