HTTP和MQTT协议实践

一、读取指定城市的天气预报信息

1.创建java project
2.创建package
3.创建class
4.写入代码
代码如下:

package TQ;
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 tq {
	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+"></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;
		   }

		   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");
		   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 {
		  System.out.println(getWeather("重庆"));   
		 }


}

运行结果如下
在这里插入图片描述

二、给指定手机号码发送验证码

1.创建java project
2.创建package
3.创建class
4.写入代码
代码如下:

package com.kkb.com;
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 demo1 {
	public static void main(String[] args) throws IOException {
		 //短信api 服务器地址https://itdage.com/kkb/kkbsms?key =xzk&number = 15123867650&code = 666666
		//图灵机器人服务器地址:https://api.jisuapi.com/iqa/query?appkey = 62958a3a6ef3c56d&queation =""
		//准备一个URL类的对象
		//String question =URLEncoder.encode("","UTF-8");
		URL url = new URL("https://itdage.com/kkb/kkbsms?key=xzk&number=15123867650&code=666666");
		//URL url = new URL("https://api.jisuapi.com/iqa/query?appkey=62958a3a6ef3c56d&question=杭州天气");
		//String question =URLEncoder.encode("给爷讲一个笑话","UTF-8");
		//打开服务器连接, 得到连接的对象 conn
		URLConnection conn = url.openConnection();
		//获取加载数据的字节输入流is
		InputStream is =  conn.getInputStream();
		//将is装饰为能一次读取一行的字符输入流
		BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
		//加载一行数据
		String text1 = br.readLine();
		//显示
		System.out.println(text1);
		//释放资源
		//String text2 = br.readLine();
		//显示
		//System.out.println(text2);
		br.close();
		
	}

}

运行结果
在这里插入图片描述
在这里插入图片描述

三、学习和熟悉MQTT 协议

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值