更新时间:2022-11-28 11:23:43 来源:极悦 浏览1005次
打开 Apache NetBeans IDE
单击“新项目...”
创建一个新的 Java 应用程序
命名项目和包
创建一个新的 Java 类
复制粘贴下面的示例源代码
运行 Java 应用程序
如果需要,选择主类
以下用 Java 编程语言编写的示例源代码可以免费使用,您可以将其简单地实现到您的项目中,也可以修改源代码以将其用于其他项目或应用程序。如果您想运行此示例代码,您只需创建一个具有单个 Java 类的新 Java 应用程序并运行该项目,如您在上面的步骤说明和视频中看到的那样。
package tester;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpApiTester {
public static void main(String[] args) {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (GeneralSecurityException e) {
System.out.println(e.getMessage());
}
try {
StringBuilder sendString = new StringBuilder();
String username = "john";
String password = "Xc3ffs";
String messagetype = "SMS:TEXT";
String httpUrl = "http://127.0.0.1:9508/";
String recipient = URLEncoder.encode("+36201324567", "UTF-8");
String messagedata = URLEncoder.encode("TestMessage", "UTF-8");
sendString.append(httpUrl).append("api?action=sendmessage").
append("&username=").append(username).append("&password=").
append(password).append("&recipient=").append(recipient).
append("&messagetype=").append(messagetype).append("&messagedata=").
append(messagedata);
System.out.println("Sending request: " + sendString.toString());
URL url = new URL(sendString.toString());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader br = null;
System.out.println("Http response received: ");
if (con.getResponseCode() == 200) {
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
System.out.println(strCurrentLine);
}
} else {
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
System.out.println(strCurrentLine);
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
以上就是关于“Java实现发送短信的代码”介绍,大家如果想了解更多相关知识,不妨来关注一下本站的Java极悦在线学习,里面的课程内容从入门到精通,细致全面,很适合没有基础的小伙伴学习,希望对大家能够有所帮助。
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习