java自动发送的代码,需要自记编译

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DataSender {
    public static void main(String[] args) {
        String targetUrl = "http://example.com/api/data"; // 目标URL
        String data = "Hello, World!"; // 要发送的数据

        try {
            URL url = new URL(targetUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");

            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(data.getBytes());
            outputStream.flush();

            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于微信没有提供官方的 API 来实现自动推送消息,因此需要使用第三方的库来模拟微信客户端的操作。下面是一个使用 Java 和 wechaty-puppet-padplus 库实现自动推送消息的例子: ``` import io.github.wechaty.Wechaty; import io.github.wechaty.WechatyOptions; import io.github.wechaty.filebox.FileBox; import io.github.wechaty.user.Contact; import io.github.wechaty.user.Message; import io.github.wechaty.user.Room; import io.github.wechaty.user.UrlLink; public class WechatyAutoPush { public static void main(String[] args) { // 初始化 wechaty 客户端 WechatyOptions options = new WechatyOptions(); options.setPuppet("wechaty-puppet-padplus"); options.setPuppetOptions(new PuppetOptions().setToken("YOUR_PADPLUS_TOKEN")); Wechaty bot = new Wechaty(options); // 注册消息监听器 bot.onMessage(message -> { String text = message.text(); if (text.equalsIgnoreCase("推送消息")) { pushMessage(message); } }); // 启动 wechaty 客户端 bot.start(); } /** * 推送消息到微信群 */ private static void pushMessage(Message message) { Room room = message.room(); if (room != null) { Contact contact = room.alias("YOUR_GROUP_ALIAS"); if (contact != null) { String title = "自动推送消息"; String description = "这是一条自动推送的消息"; String url = "https://www.example.com"; UrlLink urlLink = new UrlLink(title, description, url, null); FileBox fileBox = FileBox.fromUrl(url, title); contact.say(urlLink); contact.say(fileBox); } } } } ``` 这个例子使用了 wechaty-puppet-padplus 库来连接微信客户端,并实现了一个简单的自动推送消息的功能。当收到用户发送的 "推送消息" 消息时,会自动将一条 UrlLink 和一个文件发送到指定的微信群中。其中 wechaty-puppet-padplus 库需要在项目的依赖中添加,以便进行编译和运行。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值