如何使用Java发送cURL -X POST --data-urlencode [英] How to send cURL -X POST --data-urlencode with Java

查看:1487
本文介绍了如何使用Java发送cURL -X POST --data-urlencode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在互联网上寻找这个问题,但没有找到一个可行的答案。我正在尝试制作一个bukkit插件,以便在运行命令时将数据发送到传入的Slack webhook。我已经注意到命令正在运行,但是我不知道如何发送JSON。 (对于不熟悉Slack的用户,终端窗口中的命令为 curl -X POST --data-urlencode'payload = { channel:#slack-channel-id,用户名:机器人的用户名,文本:自我解释, icon_emoji:机器人的图标}'https://slack.com/custom/webhook/token/here 我'一直到处搜寻,并努力寻找一个Java时间来发送此消息的好时机。但是无论我如何尝试都行不通。不胜感激,谢谢

I've been looking all over the internet for this, and I just haven't found an answer that works. I'm trying to make a bukkit plugin that sends data to an ingoing Slack webhook when a command is run. I've gotten to noticing the command running, but I have no idea how to send the JSON. (For those of you unfamiliar with Slack, the command inside a terminal window is curl -X POST --data-urlencode 'payload={"channel":"#slack-channel-id","username":"bot's username","text":"Self explanatory","icon_emoji":"The bot's icon"}' https://slack.com/custom/webhook/token/here I've been looking all over and googling for a good hour trying to find a way in Java to send this. But no matter what I try it doesn't work. Any help is appreciated, thanks

推荐答案

//您可以使用以下代码起作用!
slackWebhook是您可以从custom_integration链接

//You can use the following code it works! slackWebhook is the https endpoint for the channel that you can get from custom_integration link

    String payload = "payload={\"channel\": \"#channel_name\", \"text\": \"This is posted "
            + "to #ewe_gps_abacus_notif and comes from a bot named change-alert.\"}";

    StringEntity entity = new StringEntity(payload,
            ContentType.APPLICATION_FORM_URLENCODED);

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(slackWebhook);
    request.setEntity(entity);

    HttpResponse response = null;
    try {
        response = httpClient.execute(request);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(response.getStatusLine().getStatusCode());

这篇关于如何使用Java发送cURL -X POST --data-urlencode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆