using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
namespace ZCJ
{
public partial class SendPhoneMsg
{
/// <summary>
/// 数据库调用发送手机消息
/// </summary>
/// <param name="Msg">长度195</param>
/// <param name="Tel">手机号</param>
/// <returns></returns>
[SqlFunction(IsDeterministic = true, DataAccess = DataAccessKind.None)]
public static SqlString SendMsg(SqlString Msg, SqlString Tel)
{
try
{
string message = Msg.Value,
username = "接口名",
password = "接口密码",
url = "接口地址";
byte[] byteArray = Encoding.UTF8.GetBytes("mobile=" + Tel.Value + "&message=" + message);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(username + ":" + password));
webRequest.Headers.Add("Authorization", auth);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.Default);
string Message = php.ReadToEnd();//{"error":0,"msg":"ok"}
var result = "1:发送成功";
if (Message.IndexOf("ok") > 0)
result = "1:发送成功";
else
result = "0:发送失败";
return (SqlString)result;
}
catch (Exception ex)
{
return (SqlString)"0:发送失败" + ex.Message;
}
}
}
}
请发表评论