极光速推的文档中MD5机密,采用的是32位的加密模式(文档没有写清楚,感谢QQ群的大神解答).
MD5加密代码(C#)
/// <summary>
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <returns>加密后的字串</returns>
public static string MD5Encrypt(string strSource)
{
return MD5Encrypt(strSource, 32);
}
/// <summary>
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
/// <returns>加密后的字串</returns>
public static string MD5Encrypt(string strSource, int length)
{
byte[] bytes = Encoding.ASCII.GetBytes(strSource);
byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
switch (length)
{
case 16:
for (int i = 4; i < 12; i++)
sb.Append(hashValue[i].ToString("x2"));
break;
case 32:
for (int i = 0; i < 16; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
default:
for (int i = 0; i < hashValue.Length; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
}
return sb.ToString();
}
C#与极光速推服务器连接的其实就是远程产生httppost连接,连接方式之前的博文已经说明了:
简单的调用示例为:
public static string doSend(){
IDictionary<string, string> parameters = new Dictionary<string, string>();
string html=string.Empty;
int sendno = 1;
string receiverValue = "test";//这个是一个别名
int receiverType = 3;
string md5Password = MD5Encrypt(password).ToUpper();
string input = string.Empty;
input = username + sendno + receiverType + receiverValue + md5Password;
string verificationCode = MD5Encrypt(input).ToUpper();
string content = "{\"n_content\":\"" + "test" + "\"}"; //发送的内容
parameters.Add("username",username);
parameters.Add("sendno",sendno.ToString());
parameters.Add("appkeys",appkeys);
parameters.Add("receiver_type", receiverType.ToString());
parameters.Add("receiver_value", receiverValue.ToString());
parameters.Add("verification_code",verificationCode); //MD5
parameters.Add("msg_type", "1");
parameters.Add("msg_content", content); //内容
parameters.Add("platform", "android");
HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, Encoding.UTF8, null);
if (response!=null)
{
// 得到返回的数据流
Stream receiveStream = response.GetResponseStream();
// 如果有压缩,则进行解压
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress);
}
// 得到返回的字符串
html = new StreamReader(receiveStream).ReadToEnd();
}
return html;
}
/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}