HTTP协议实践
读取指定城市的天气预报信息
package weatherDemo;
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+"></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("重庆"));
}
}
给指定手机发送验证码
阿里云服务器实现代码
package sendPhoneMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
public class QuerySendDetails {
//自己的AccessKey ID(不要泄露)
static final String accessKeyId = "AccessKey ID";
//自己的AccessKeySecret(不要泄露)
static final String accessKeySecret = "AccessKeySecret";
//接收验证码的手机号
static final String tlephone = "xxxxxxxxxxx";
//签名名称
static final String signName = "签名名称";
//短信模板ID。请在控制台模板管理页面模板CODE一列查看。
static final String templateCode = "签名CODE";
/**
* 生成四位随机数验证码
* @return
*/
static String setRandomNumber() {
return Integer.toString(((int)(Math.random()*9000+1000)));
}
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-发送号码服务商地区(精确到城市)", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "cn-发送号码服务商地区");
request.putQueryParameter("PhoneNumbers", tlephone);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", "{\"code\":\""+setRandomNumber()+"\"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
KKB.COM服务器代码
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=发送手机号码&code=wangyuxi");
//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();
}
}