首页 > Web开发 > 详细

webhook

时间:2020-07-18 11:34:12      阅读:53      评论:0      收藏:0      [点我收藏+]

webhook是一种http post的请求。进行消息通知。

下面进行代码演示

引入依赖

技术分享图片

 

 

package t4;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
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.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

class student {
private String name;
private String sex;

public student(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

}

public class Webhook {
private static String WEB_HOOK = "https://webhook.site/3dc40ac3-03b4-492a-a9ca-9d580c6780ce";

public static void main(String[] args) throws ClientProtocolException, IOException {

HttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(WEB_HOOK);
httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
// 构建一个json格式字符串textMsg,其内容是接收方需要的参数和消息内容
student student = new student("xiaoMing", "man");
String msg = JSON.toJSONString(student);
StringEntity se = new StringEntity(msg, "utf-8");
httpPost.setEntity(se);
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
}
}

}

在设置的回调地址处可以进行消息查看

技术分享图片

 

webhook

原文:https://www.cnblogs.com/dengw125792/p/13334420.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!