HTTP和MQTT协议实践
一、REST协议实现
任务要求一
安装Java开发环境和Java IDE编程工具 Eclipse 或 IDEA,基于HTTP协议(严格地说是 “REST接口规范”)读取互联网上web服务网站实现:
1)读取指定城市的天气预报信息;
2)给指定手机号码发送验证码;
1. 读取指定城市的天气预报信息
代码如下
package tianqi;
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 WeatherDemo {
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:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("<soap:Body><"+method+" xmlns=\"http://WebXml.com.cn/\"><"+paramName+">" + paramValue + "</"+paramName+"></"+method+