C#
Post.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Post.aspx.cs" Inherits="Post" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>云信互联DEMO</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table bordercolor="#dcdcdc" cellpadding="4" cellspacing="0" frame="box" rules="none"
style="border-collapse: collapse">
<tr>
<td background="#dcdcdc" class="frmHeader" style="border-right: white 2px solid">参数</td>
<td background="#dcdcdc" class="frmHeader">值</td>
</tr>
<tr>
<td class="frmText" style="font-weight: normal; color: #000000">account:</td>
<td><asp:TextBox CssClass="frmInput" Id="Txtaccount" runat="server" Width="272px" /></td>
</tr>
<tr>
<td class="frmText" style="font-weight: normal; color: #000000">password:</td>
<td><asp:TextBox CssClass="frmInput" id="Txtpassword" runat="server" Width="272px" /></td>
</tr>
<tr>
<td class="frmText" style="font-weight: normal; color: #000000">phone:</td>
<td>
<asp:TextBox CssClass="frmInput" id="Txtmobile" runat="server" name="scorpid" Width="272px" /></td>
</tr>
<tr>
<td class="frmText" style="font-weight: normal; color: #000000">msg:</td>
<td>
<asp:TextBox CssClass="frmInput" id="Txtcontent" runat="server" name="sprdid" Width="272px" /></td>
</tr>
<tr>
<td></td>
<td align="center"><asp:Button ID="ButSubmit" runat="server" Text="提交" OnClick="ButSubmit_Click" /></td>
</tr>
<tr>
<td></td>
<td align="center"><asp:Label ID="LabelRetMsg" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Post.aspx.cs
using System;
using System.Security.Cryptography;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
public partial class Post : System.Web.UI.Page
{
public static string PostUrl = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButSubmit_Click(object sender, EventArgs e)
{
string un = this.Txtaccount.Text.Trim();
string pw = this.Txtpassword.Text.Trim();
pw= getMd5Hash(pw);
string phone = Txtmobile.Text.Trim();
string content =Txtcontent.Text.Trim();
string url = "https://u.smsyun.cc/sms-partner/access/"+un+"/sendsms";
string data="{"smstype":"4","clientid":""+un+"","password":""+pw+"","mobile":""+phone+"","content":""+content+"","sendtime":""+DateTime.Now.Date.ToString()+"","extend":"00","uid":"00"}";
string result = doPostMethodToObj(url, data); //调用POST方式
LabelRetMsg.Text = result+"<br>code===0代表发送成功";
}
//POST方式接口
public static string doPostMethodToObj(string url, string data) {
string result = String.Empty;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
result = streamReader.ReadToEnd();
}
}
return result;
}
// md5加密
public static string getMd5Hash(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
}
Web.Config
<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config 中
WebReference.Service.PostUrl 短信发送接口
WebReference.Service.BalanceQueryUrl 获取余额接口
-->
<configuration>
<appSettings>
<add key="WebReference.Service.PostUrl" value="https://u.smsyun.cc/sms-partner/access/{您的账号}/sendsms"/>
</appSettings>
<connectionStrings/>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true"/>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<!-- <authentication mode="Windows"/> -->
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>