webhook

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

}

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

 

posted @ 2020-07-18 05:46  工设091  阅读(354)  评论(0编辑  收藏  举报