使用JAVA环境基于HTTP协议实现以下功能
1)读取指定城市的天气预报信息;2)给指定手机号码发送验证码;
1.读取指定城市的天气信息
1)
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 Demo4 {
public static void main(String[] args) throws IOException {
//关键使用步骤:
//0. 将发送给图灵机器人的文字转化为URL编码
String question = URLEncoder.encode("重庆天气", "UTF-8");
//1. 先准备一个URL类的对象 u
URL url = new URL("https://api.jisuapi.com/iqa/query?appkey=62958a3a6ef3c56d&question="+question);
//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();
}
}
这里调用了智能机器人的API
结果
2)
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("重庆"));
}
}
2.给指定手机号码发送验证码
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=150xxxxxx5&code=123456789");
//,发送短信给150xxxxxx5,验证码为123456789
//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();
}
}