技术支撑 > 帮助中心 > 代码示例 > JSP

JSP DEMO


<%@ page language="java" import="java.util.*,java.io.*,java.net.*" contentType="text/html; charset=GB2312" pageEncoding="GB2312"%><%
String UserName,Password,Mobiles,Content;
UserName = (request.getParameter("UserName")).trim().replace("'","''");//用户帐号
Password = (request.getParameter("Password")).trim().replace("'","''");//用户密码
Mobiles = (request.getParameter("Mobiles")).trim().replace("'","''");//接收号码
Content = (request.getParameter("Content")).trim().replace("'","''");//发送内容
String strUrl = "http://api.sms1086.com/api/Send.aspx?username="+URLEncoder.encode(UserName,"GB2312")+"&password="+URLEncoder.encode(Password,"GB2312")+"&mobiles="+URLEncoder.encode(Mobiles,"GB2312")+"&content="+URLEncoder.encode(Content,"GB2312");
URL url = null;
try
{
  url = new URL(strUrl);
  URLConnection UConn = url.openConnection();  
  BufferedReader breader = new BufferedReader(new InputStreamReader(UConn.getInputStream()));
  String str=breader.readLine(); 
  while(str != null){   
   str = URLDecoder.decode(str,"GB2312");
   String[] strs=str.split("&");
   if(strs[0].replace("result=","").trim().equals("0"))
   {
   	str = strs[2].replace("description=","");
   }
   else
   {
   str = "发送失败。失败原因:"+strs[1].replace("description=","");
   }
   PrintWriter outa  = response.getWriter();
   outa.println(str);
   str=breader.readLine();   
  }
 
}
catch(Exception e)
{
}
%>
 CSHARP:
using System;
using System.Collections.Generic;
using System.Text;
// 提示: 需要引用"System.Web.dll";
namespace ApiDemo
{
    class Program
    {
        /// 
        /// 内容编码(GB2312)
        /// 
        static Encoding content_encoding = Encoding.GetEncoding("GB2312");

        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("请输入操作:S(发送短信),Q(查询余额),C(修改密码) X(退出)");
                string cmd = Console.ReadLine().ToUpper();
                if (cmd == "S")
                {
                    SendSmsDemo();
                }
                else if (cmd == "Q")
                {
                    QueryDemo();
                }
                else if (cmd == "C")
                {
                    ChgPwdDemo();
                }
                else if (cmd == "X")
                {
                    break;
                }
            }
        }

        /// 
        /// 发送短信的演示代码
        /// 
        static void SendSmsDemo()
        {
            Console.Write("请输入用户名:");
            string username = Console.ReadLine();
            Console.Write("请输入密码:");
            string password = Console.ReadLine();
            Console.Write("请输入目标号码(多个号以逗号分开):");
            string mobiles = Console.ReadLine();
            Console.Write("请输入发送内容:");
            string content = Console.ReadLine();
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Encoding = content_encoding;
            string result = wc.DownloadString(
                string.Format(
                "http://api.sms1086.com/api/Send.aspx?username=0&password=1&mobiles=2&content=3",
                System.Web.HttpUtility.UrlEncode(username, content_encoding),
                System.Web.HttpUtility.UrlEncode(password, content_encoding),
                System.Web.HttpUtility.UrlEncode(mobiles, content_encoding),
                System.Web.HttpUtility.UrlEncode(content, content_encoding)
                )
            );

            Dictionary response = DecodeResponse(result);
            Console.WriteLine("发送返回码为:" + response["result"]);
            Console.WriteLine("错误描述为:" + response["description"]);
        }


        /// 
        /// 查询余额的演示代码
        /// 
        static void QueryDemo()
        {
            Console.Write("请输入用户名:");
            string username = Console.ReadLine();
            Console.Write("请输入密码:");
            string password = Console.ReadLine();
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Encoding = content_encoding;
            string result = wc.DownloadString(
                string.Format(
                "http://api.sms1086.com/api/Query.aspx?username=0&password=1",
                System.Web.HttpUtility.UrlEncode(username, content_encoding),
                System.Web.HttpUtility.UrlEncode(password, content_encoding)
                )
            );

            Dictionary response = DecodeResponse(result);

            Console.WriteLine("查询余额返回码为:" + response["result"]);
            Console.WriteLine("错误描述为:" + response["description"]);
            if(response["result"] == "0")
            {
                // 如果返回码为0, 则返回的信息中还包含一个blance字段
                Console.WriteLine("当前余额为:" + response["balance"]);
            }
        }

        /// 
        /// 修改密码的演示代码
        /// 
        static void ChgPwdDemo()
        {
            Console.Write("请输入用户名:");
            string username = Console.ReadLine();
            Console.Write("请输入密码:");
            string password = Console.ReadLine();
            Console.Write("请输入新密码:");
            string new_password = Console.ReadLine();
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Encoding = content_encoding;

            string result = wc.DownloadString(
                string.Format(
                    "http://api.sms1086.com/api/ChgPwd.aspx?username=0&password=1",
                    System.Web.HttpUtility.UrlEncode(username, content_encoding),
                    System.Web.HttpUtility.UrlEncode(password, content_encoding)
                )
            );

            Dictionary response = DecodeResponse(result);

            Console.WriteLine("修改密码返回码为:" + response["result"]);
            Console.WriteLine("错误描述为:" + response["description"]);
        }

        /// 
        /// 对返回的结果解码
        /// 
        /// 返回结果
        /// 可通过字符串索引的结果值
        static Dictionary DecodeResponse(string result)
        {
            string[] responses = result.Split('&');

            // 解码返回的字段列表
            Dictionary response = new Dictionary();
            foreach (string pair in responses)
            {
                string[] kv = pair.Split('=');
                if (kv.Length == 2)
                {
                    response[kv[0]] = System.Web.HttpUtility.UrlDecode(kv[1], content_encoding);
                }
            }
            return response;
        }
    }
}