@[TOC](目录)
Java获取指定城市的天气预报
- 任务概况
任务名称: 验证码短信发送 任务概述: 通过Java程序,向用户输入的手机号码发送短信验证码。
- 网络组成分析
网址的组成: 协议://域名:端口号/虚拟路径?参数列表#锚点 协议:
用于计算机与计算机之间交流的协议,用于传输速率,传输编码,出错的控制等等。 http:超文本传输协议,默认端口号:80
https:安全超文本传输协议,默认端口号:443域名:ip地址的别名。ip地址是计算机在互联网中的唯一表示,192.168.1.1
端口号:范围:0-65535.
虚拟路径:通过路径的方式,来管理服务器中的文件资源。
参数列表:向服务器发送的数据,每一个参数都是一个键值对,键和值之间通过=号连接,多个键值对之间通过&分割。
- URL类
//关键使用步骤:
//1. 先准备一个URL类的对象 u
URL url = new URL(“网址内容”);
//2. 打开服务器连接,得到连接对象 conn
URLConnection conn = url.openConnection();
//3. 获取加载数据的字节输入流 is
InputStream is = conn.getInputStream();
//4. 将is装饰为能一次读取一行的字符输入流 br
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//5. 加载一行数据
String text = br.readLine();
//6. 显示
System.out.println(text);
//7. 释放资源
br.close();
- 编码表
数字:
65:A
66:B
97: a
- 具体代码:
package com.kkb.demo;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class WeatherInfo {
//method 要调用的webservie方法名,paramName请求参数名,paramValue请求参数值
private static String makeSoapRequest(String method,String paramName,String paramValue) {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
sb.append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
sb.append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
// 构造请求内容,这里需要将XXX替换为webservice的地址
sb.append("<soap:Body><"+method+" xmlns=\"http://WebXml.com.cn/\"><"+paramName+">" + paramValue + "</"+paramName+"></"+method+"></soap:Body>");
sb.append("</soap:Envelope>");
return sb.toString();
}
private static InputStream getSoapInputStream(String method,String paramName,String paramValue) throws Exception {
try {
String soap = makeSoapRequest(method,paramName,paramValue);
if (soap == null) {
return null;
}
// 需要替换成webservice的完整地址
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getWeather(String city) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream("getWeatherbyCityName","theCityName",city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#") ;
break ;
}
sb.append(n.getFirstChild().getNodeValue() + "#\n");
}
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws ParseException {
//参数为要查询的planid
System.out.println(getWeather("重庆"));
}
}`
- 运行结果如下
Java发送短信
- API列表
短信API
短信API服务器地址:
https://itdage.com/kkb/kkbsms
参数列表
key:秘钥,
值为:xzk number:
接收验证码的手机号 code:
A-Za-z0-9的验证码内容。
具体代码如下:
package com.kkb.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Demo5 {
public static void main(String[] args) throws IOException {
//关键使用步骤:
//1. 先准备一个URL类的对象 u
URL url = new URL("https://itdage.com/kkb/kkbsms?key=xzk&number=18983921532&code=panxiang631807060523");
//,发送短信18983921532,15084421075@1637489785322482.onaliyun.com
//2. 打开服务器连接,得到连接对象 conn
URLConnection conn = url.openConnection();
//3. 获取加载数据的字节输入流 is
InputStream is = conn.getInputStream();
//4. 将is装饰为能一次读取一行的字符输入流 br
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
//5. 加载一行数据
String text = br.readLine();
//6. 显示
System.out.println(text);
//7. 释放资源
br.close();
}
}
由于短信是发送到老师手机上的,故不予展示。代码是可以运行成功的。