融云及時聊天Server API發(fā)送消息通用幫助類

可解決服務(wù)器證書驗證問題

獲取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發(fā)送消息通用幫助類
        /// </summary>
        /// <param name="methodUrl">融云接口請求地址</param>
        /// <param name="postStr">發(fā)送數(shù)據(jù)串</param>
        /// <param name="responseArray">返回數(shù)據(jù)</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();//隨機(jī)數(shù)
            string timestamp = ConvertDateTimeInt(DateTime.Now).ToString();//獲取時間戳
            string signature = GetHash(appSecret   nonce   timestamp);//數(shù)據(jù)簽名
            //解決服務(wù)器證書驗證問題
            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時間格式轉(zhuǎn)換為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轉(zhuǎn)換成byte[]
            UTF8Encoding enc = new UTF8Encoding();
            byte[] dataToHash = enc.GetBytes(input);

            //Hash運(yùn)算
            byte[] dataHashed = sha.ComputeHash(dataToHash);

            //將運(yùn)算結(jié)果轉(zhuǎn)換成string
            string hash = BitConverter.ToString(dataHashed).Replace("-", "");

            return hash;
        }
    }
}


原文鏈接:融云及時聊天Server API發(fā)送消息通用幫助類