由于楼主学的是C#,所以目前做的百度短信API是C#版的,废话不说了,直接上代码。

 public void PostData()
{
string url = "http://sms.bj.baidubce.com/bce/v2/message";
//string url = "http://sms.bj.baidubce.com/v1/message";
string ak = "*******";
string sk = "*******";
string jsonStr = "{\"invokeId\":\"调用ID\",\"phoneNumber\":\"18102301717\",\"templateCode\":\"smsTpl:b43d4ccc-7ca6-49dd-90af-faea6b4f2190\",\"contentVar\":{ \"feeType\": \"物业费\",\"fee\":\"80\"}}";
HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
Uri uri = httpRequest.RequestUri;
httpRequest.Method = "POST";
HttpWebResponse httpResponse = null;
DateTime now = DateTime.Now;
int expirationInSeconds = ;
string signDate = now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK");
string authString = "bce-auth-v1/" + ak + "/" + signDate + "/" + expirationInSeconds;
string signingKey = Hex(new HMACSHA256(Encoding.UTF8.GetBytes(sk)).ComputeHash(Encoding.UTF8.GetBytes(authString)));
string canonicalRequestString = CanonicalRequest(httpRequest);
string signature = Hex(new HMACSHA256(Encoding.UTF8.GetBytes(signingKey)).ComputeHash(Encoding.UTF8.GetBytes(canonicalRequestString)));
string authorization = authString + "/host/" + signature;
httpRequest.ContentType = "application/json";
httpRequest.KeepAlive = false;
httpRequest.Headers["Authorization"] = authorization;
httpRequest.Headers["x-bce-content-sha256"] = signingKey;
httpRequest.Headers["x-bce-date"] = signDate;
if ( < jsonStr.Length)
{
byte[] data = Encoding.UTF8.GetBytes(jsonStr);
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(data, , data.Length);
}
}
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (WebException ex)
{
httpResponse = (HttpWebResponse)ex.Response;
}
Console.WriteLine(httpResponse.StatusCode);
Console.WriteLine(httpResponse.Method);
Console.WriteLine(httpResponse.Headers);
Stream st = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("\n");
}
static string UriEncode(string input, bool encodeSlash = false)
{
StringBuilder builder = new StringBuilder();
foreach (byte b in Encoding.UTF8.GetBytes(input))
{
if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '' && b <= '') || b == '_' || b == '-' || b == '~' || b == '.')
{
builder.Append((char)b);
}
else if (b == '/')
{
if (encodeSlash)
{
builder.Append("%2F");
}
else
{
builder.Append((char)b);
}
}
else
{
builder.Append('%').Append(b.ToString("X2"));
}
}
return builder.ToString();
} static string Hex(byte[] data)
{
var sb = new StringBuilder();
foreach (var b in data)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
} static string CanonicalRequest(HttpWebRequest req)
{
Uri uri = req.RequestUri;
StringBuilder canonicalReq = new StringBuilder();
canonicalReq.Append(req.Method).Append("\n").Append(UriEncode(Uri.UnescapeDataString(uri.AbsolutePath))).Append("\n"); var parameters = HttpUtility.ParseQueryString(uri.Query);
List<string> parameterStrings = new List<string>();
foreach (KeyValuePair<string, string> entry in parameters)
{
parameterStrings.Add(UriEncode(entry.Key) + '=' + UriEncode(entry.Value));
}
parameterStrings.Sort();
canonicalReq.Append(string.Join("&", parameterStrings.ToArray())).Append("\n"); string host = uri.Host;
if (!(uri.Scheme == "https" && uri.Port == ) && !(uri.Scheme == "http" && uri.Port == ))
{
host += ":" + uri.Port;
}
canonicalReq.Append("host:" + UriEncode(host));
return canonicalReq.ToString();
}
04-30 20:31