示例程序
示例代码仅供学习和研究小智 IoT 接口使用,
只是为了方便开发者测试而提供的样例代码,
开发者可以根据自己的业务需要,
按照开发文档编写,并非一定要使用该代码。
示例说明
以下为各开发语言 HTTP POST 示例
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
/**
*
* JAVA 示例
*
* @author chenfan
* @version 1.0, 2018/10/10
*
*/
public class HTTPTest {
// 开发者参数
public String devId = "c153cca0d4024399819e4b1df7a12dcc";
public String devKey = "d0ae391e48d24b5b936c29d334c67398";
// 转码
public String encode(String input) throws Exception {
return URLEncoder.encode(input, "UTF-8");
}
// 计算 MD5 值
public static String md5(String input) throws Exception {
byte[] digest = MessageDigest.getInstance("MD5").digest(input.getBytes("UTF-8"));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
sb.append(String.format("%02x", Integer.valueOf(digest[i] & 0xFF)));
}
return sb.toString();
}
// 发起 POST 请求
public String post(String url, String data) throws Exception {
// 打开连接
HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.connect();
// 输出参数
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(data);
dos.flush();
dos.close();
// 读取响应
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = br.readLine();
br.close();
// 关闭连接
conn.disconnect();
return line;
}
// 短信发送接口
public void send() throws Exception {
// 参数
String url = "http://www.xinxinke.com/api/send";
String smsTemplateCode = "T_CODE_20160629";
String smsParam = "{\"code\":\"" + Math.random() + "\"}";
String recNum = "13800138000";
String sign = md5(devId + devKey + recNum);
// 拼接
StringBuffer data = new StringBuffer();
data.append("").append("dev_id").append("=").append(encode(devId));
data.append("&").append("sign").append("=").append(encode(sign));
data.append("&").append("sms_template_code").append("=").append(encode(smsTemplateCode));
data.append("&").append("sms_param").append("=").append(encode(smsParam));
data.append("&").append("rec_num").append("=").append(encode(recNum));
// 请求
System.out.println(post(url, data.toString()));
}
// 短信发送状态报告接口
public void report() throws Exception {
// 参数
String url = "http://www.xinxinke.com/api/report";
String sign = md5(devId + devKey);
// 拼接
StringBuffer data = new StringBuffer();
data.append("").append("dev_id").append("=").append(encode(devId));
data.append("&").append("sign").append("=").append(encode(sign));
// 请求
System.out.println(post(url, data.toString()));
}
// 短信接收接口
public void receive() throws Exception {
// 参数
String url = "http://www.xinxinke.com/api/receive";
String sign = md5(devId + devKey);
// 拼接
StringBuffer data = new StringBuffer();
data.append("").append("dev_id").append("=").append(encode(devId));
data.append("&").append("sign").append("=").append(encode(sign));
// 请求
System.out.println(post(url, data.toString()));
}
public static void main(String[] args) throws Exception {
HTTPTest t = new HTTPTest();
// t.send();
// t.report();
t.receive();
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace HTTPTest
{
class Program
{
static void Main(string[] args)
{
string dev_id = "c153cca0d4024399819e4b1df7a12dcc"; // 开发者标识
string dev_key = "d0ae391e48d24b5b936c29d334c67398"; // 开发者密钥
string sign = md5(dev_id + dev_key);
string url = "http://www.xinxinke.com/api/account";
// 如在发短信的时候中文参数,请将参数编码:encode(中文)
string postDataStr = string.Format("dev_id={0}&sign={1}", dev_id, sign);
string res = post(url, postDataStr);
Console.Write(res);
Console.ReadLine();
}
// 发送请求
public static string post(string url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// 发送请求
Stream myRequestStream = request.GetRequestStream();
byte[] data = System.Text.Encoding.UTF8.GetBytes(postDataStr);
myRequestStream.Write(data, 0, data.Length);
myRequestStream.Flush();
myRequestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 获取返回数据
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
// md5加密32位.
public static string md5(String s)
{
try
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return ret.PadLeft(32, '0');
}
catch
{
}
return "";
}
// 中文转码-UTF8
public static string encode(string str)
{
if (string.IsNullOrEmpty(str))
return null;
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
}
}
}
<?php
$dev_id = "c153cca0d4024399819e4b1df7a12dcc";
$dev_key = "d0ae391e48d24b5b936c29d334c67398";
$rec_num = "13800138000,18900189000,18600186000";
$sign = $dev_id.$dev_key.$rec_num;
$para = "dev_id=".$dev_id;
$para.= "&sign=".md5($sign);
$para.= "&sms_template_code=T_CODE_20160629";
$para.= "&rec_num=".$rec_num;
$para.= "&sms_param={\"code\":\"123456\"}";
$curl = curl_init("http://www.xinxinke.com/api/send");
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $para);
echo(curl_exec($curl));
curl_close($curl);
?>
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
require "net/http"
require "digest/md5"
dev_id = "c153cca0d4024399819e4b1df7a12dcc"
dev_key = "d0ae391e48d24b5b936c29d334c67398"
rec_num = "13800138000,18900189000,18600186000"
sign = "#{dev_id}#{dev_key}#{rec_num}"
para = {}
para["dev_id"] = "#{dev_id}"
para["sign"] = Digest::MD5.hexdigest("#{sign}")
para["sms_template_code"] = "T_CODE_20160629"
para["rec_num"] = "#{rec_num}"
para["sms_param"] = "{\"code\":\"123456\"}"
url = URI.parse("http://www.xinxinke.com/api/send")
resp = Net::HTTP.post_form(url, para)
puts resp.body
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib
import urllib2
import hashlib
# 开发者参数
devId = "c153cca0d4024399819e4b1df7a12dcc"
devKey = "d0ae391e48d24b5b936c29d334c67398"
# 接收号码
recNum = "13800138000"
# 计算签名
md = hashlib.md5();
md.update(devId + devKey + recNum)
sign = md.hexdigest()
# 请求参数
data = {}
data["dev_id"] = devId
data["sign"] = sign
data["sms_template_code"] = "dynamic_code"
data["sms_param"] = "{\"code\":\"123456\"}"
data["rec_num"] = recNum
# 发起请求
resp = urllib2.urlopen("http://www.xinxinke.com/api/send", urllib.urlencode(data))
# 输出响应
print resp.read()
ASP加密(MD5)算法下载:asp_md5.asp
<%@ LANGUAGE=VBScript CODEPAGE="65001" %>
<%
Dim url, param, http
url = "http://www.xinxinke.com/api/send"
param = "dev_id=c153cca0d4024399819e4b1df7a12dcc"
param = param & "&sign=" & ASP_MD5("c153cca0d4024399819e4b1df7a12dcc" & "d0ae391e48d24b5b936c29d334c67398" & "13800138000")
param = param & "&sms_template_code=test"
param = param & "&rec_num=13800138000"
param = param & "&sms_param={""code"":""123456"",""product"":""xinxinke""}"
Set http = Server.createobject("MSXML2.SERVERXMLHTTP.3.0")
http.Open "POST", url, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send param
Response.Write Server.HTMLEncode(http.responseText)
Set http = Nothing
%>
curl -d "dev_id=c153cca0d4024399819e4b1df7a12dcc&sign=e979f9c44f2d42a5895bfc92ab2408de&sms_template_code=T_CODE_20160629&sms_param={\"code\":\"123456\"}&rec_num=13800138000" "http://www.xinxinke.com/api/send"
wget --post-data "dev_id=c153cca0d4024399819e4b1df7a12dcc&sign=e979f9c44f2d42a5895bfc92ab2408de&sms_template_code=T_CODE_20160629&sms_param={\"code\":\"123456\"}&rec_num=13800138000" "http://www.xinxinke.com/api/send"
.版本 2
.子程序 子_http
.局部变量 局_http, 对象
.局部变量 局_Body, 变体型
局_http.创建 (“WinHttp.WinHttpRequest.5.1”)
局_http.方法 (“Open”, “POST”, “http://www.xinxinke.com/api/send”)
局_http.方法 (“SetRequestHeader”, “Content-Type”, “application/x-www-form-urlencoded”)
局_http.方法 (“SetRequestHeader”, “X-Requested-With”, “XMLHttpRequest”)
局_Body.赋值 (“dev_id=c153cca0d4024399819e4b1df7a12dcc&sign=e979f9c44f2d42a5895bfc92ab2408de&sms_template_code=T_CODE_20160629&sms_param=xxx6&rec_num=13800138000”)
局_http.方法 (“send”, 局_Body)
易语言取 MD5 值:
标准输出 (, 取数据摘要 (到字节集 (“xinxinke”)))
结果:198c3832661455c17c4fbe49c9a5dffb