融云及时聊天Server API发送消息通用帮助类

可解决服务器证书验证问题

获取AppSetings别忘记添加System.Configuration的dll引用哦


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Security.Cryptography;

namespace JsonsHelper
{
    public class RongServerHelper
    {
        /// <summary>
        /// 融云及时聊天Server API发送消息通用帮助类
        /// </summary>
        /// <param name="methodUrl">融云接口请求地址</param>
        /// <param name="postStr">发送数据串</param>
        /// <param name="responseArray">返回数据</param>
        /// <remarks>
        /// 获取AppSetings别忘记添加System.Configuration的dll引用哦
        /// </remarks>
        /// <returns></returns>
        public static string PostSendMsg(string methodUrl, string postStr, out byte[] responseArray)
        {

            string appkey = System.Configuration.ConfigurationManager.AppSettings["appkey"];//融云后台获取的appkey
            string appSecret = System.Configuration.ConfigurationManager.AppSettings["appSecret"];//融云后台获取的appSecret
            responseArray = null;
            Random rd = new Random();
            int rd_i = rd.Next();
            string nonce = rd_i.ToString();//随机数
            string timestamp = ConvertDateTimeInt(DateTime.Now).ToString();//获取时间戳
            string signature = GetHash(appSecret   nonce   timestamp);//数据签名
            //解决服务器证书验证问题
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            WebClient myWebClient = new WebClient();
            myWebClient.Headers.Add("App-Key", appkey);
            myWebClient.Headers.Add("Nonce", nonce);
            myWebClient.Headers.Add("Timestamp", timestamp);
            myWebClient.Headers.Add("Signature", signature);
            myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] byteArray = Encoding.UTF8.GetBytes(postStr);
            try
            {
                responseArray = myWebClient.UploadData(methodUrl, "POST", byteArray);
            }
            catch { }
            string resultarray = string.Empty;
            if (responseArray != null)
            {
                resultarray = Encoding.UTF8.GetString(responseArray);
            }
            return resultarray;
        }
        /// <summary>  
        /// DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time"> DateTime时间格式</param>  
        /// <returns>Unix时间戳格式</returns>  
        public static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        //SHA1加密
        public static String GetHash(String input)
        {
            //建立SHA1对象
            SHA1 sha = new SHA1CryptoServiceProvider();

            //将mystr转换成byte[]
            UTF8Encoding enc = new UTF8Encoding();
            byte[] dataToHash = enc.GetBytes(input);

            //Hash运算
            byte[] dataHashed = sha.ComputeHash(dataToHash);

            //将运算结果转换成string
            string hash = BitConverter.ToString(dataHashed).Replace("-", "");

            return hash;
        }
    }
}


原文链接:融云及时聊天Server API发送消息通用帮助类