java发送短信之AT指令
时间: 2023-11-09 14:04:58 浏览: 54
在Java中发送短信可以通过AT指令来实现。下面是一个示例代码:
```java
import java.io.*;
import java.util.*;
public class SMS {
public static void main(String[] args) throws Exception {
String recipient = "手机号码";
String message = "短信内容";
String username = "用户名";
String password = "密码";
String requestUrl = "http://www.smsadmin.cn/smsmarketing/wwwroot/api/post_send/";
String params = "un=" + username + "&pwd=" + password + "&mobile=" + recipient + "&msg=" + message;
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(params);
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
writer.close();
}
}
```
需要注意的是,这里使用的是第三方短信平台提供的API接口,需要先在该平台注册账号并获取相关参数(如用户名、密码、请求URL等)。在发送短信时,需要将手机号码和短信内容拼接成请求参数,然后通过POST请求发送到短信平台的服务器上。
阅读全文