简体   繁体   English

如何使用Java发送cURL -X POST --data-urlencode

[英]How to send cURL -X POST --data-urlencode with 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. 我正在尝试制作一个bukkit插件,该插件在运行命令时将数据发送到传入的Slack webhook。 I've gotten to noticing the command running, but I have no idea how to send the JSON. 我已经注意到命令正在运行,但是我不知道如何发送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 (对于不熟悉Slack的用户,终端窗口中的命令为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我一直在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寻找并搜寻了好一小时在Java中找到一种方法来发送此消息,但是无论我如何尝试都行不通,不胜感激,谢谢

//You can use the following code it works! //您可以使用以下代码起作用! slackWebhook is the https endpoint for the channel that you can get from custom_integration link slackWebhook是您可以从custom_integration链接获取的频道的https端点

    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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM