1. 程式人生 > >網易雲信IM C#.Net請求、操作網易雲通信ID

網易雲信IM C#.Net請求、操作網易雲通信ID

創建用戶 自動 技術含量 時間戳 action cas api sps 設定

沒啥技術含量就不寫其他的了 直接上代碼

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WXL_IM.Util
{
    public class IMUtil
    {
        #region 服務器操作URL
        // 創建【網易雲通信ID】的Url
        //  參數	    類型    必須	    說明
        //  accid	String	是	    網易雲通信ID,最大長度32字符,必須保證一個APP內唯一(只允許字母、數字、半角下劃線_、@、半角點以及半角-組成,不區分大小寫,會統一小寫處理,請註意以此接口返回結果中的accid為準)。
        //  name	String	否	    網易雲通信ID昵稱,最大長度64字符,用來PUSH推送時顯示的昵稱
        //  props	String	否	    json屬性,第三方可選填,最大長度1024字符
        //  icon	String	否	    網易雲通信ID頭像URL,第三方可選填,最大長度1024
        //  token	String	否	    網易雲通信ID可以指定登錄token值,最大長度128字符,並更新,如果未指定,會自動生成token,並在創建成功後返回
        //  sign	String	否	    用戶簽名,最大長度256字符
        //  email	String	否	    用戶email,最大長度64字符
        //  birth	String	否	    用戶生日,最大長度16字符
        //  mobile	String	否	    用戶mobile,最大長度32字符,只支持國內號碼
        //  gender	int	    否	    用戶性別,0表示未知,1表示男,2女表示女,其它會報參數錯誤
        //  ex	    String	否	    用戶名片擴展字段,最大長度1024字符,用戶可自行擴展,建議封裝成JSON字符串
        static string CRT_USER_URL = "https://api.netease.im/nimserver/user/create.action";

        // 更新【網易雲通信ID】的Url 非修改用戶信息用
        // 參數	    類型	   必須	   說明
        // accid	String	是	   網易雲通信ID,最大長度32字符,必須保證一個APP內唯一
        // props	String	否	   json屬性,第三方可選填,最大長度1024字符
        // token	String	否	   網易雲通信ID可以指定登錄token值,最大長度128字符
        static string UPD_USERID_URL = "https://api.netease.im/nimserver/user/update.action";

        // 更新並獲取新token
        // 參數	    類型	   必須	   說明
        // accid	String	是	   網易雲通信ID,最大長度32字符,必須保證一個APP內唯一
        static string UPDGET_USERTOKEN_URL = "https://api.netease.im/nimserver/user/refreshToken.action";

        // 封禁網易雲通信ID
        // 參數	    類型	   必須	   說明
        // accid	String	是	   網易雲通信ID,最大長度32字符,必須保證一個APP內唯一
        // needkick	String	否	   是否踢掉被禁用戶,true或false,默認false
        static string BLOCK_USERID_URL = "https://api.netease.im/nimserver/user/block.action";

        // 解禁網易雲通信ID
        // 參數	    類型	   必須	   說明
        // accid	String	是	   網易雲通信ID,最大長度32字符,必須保證一個APP內唯一
        static string UNBLOCK_USERID_URL = "https://api.netease.im/nimserver/user/unblock.action";
        
        #endregion 服務器操作URL

        //雲信AppKey 公匙
        static string appKey = ConfigurationManager.AppSettings["IMAppKey"];
        //雲信AppSecret 私鑰  
        static string appSecret = ConfigurationManager.AppSettings["IMAppSecret"];

        /// <summary>
        /// 執行請求
        /// </summary>
        /// <param name="action">操作碼 1創建用戶 2更新用戶ID 3更新獲取用戶token 4禁用用戶 5解禁用戶</param>
        /// <param name="reqParams">參數 key=value</param>
        /// <returns></returns>
        public static string executeRequest(int action,string reqParams)
        {
            string url = string.Empty;
            switch (action)
            {
                case 1://創建用戶
                    url = CRT_USER_URL;
                    break;
                case 2://更新用戶ID
                    url = UPD_USERID_URL;
                    break;
                case 3://更新並獲取新的Token
                    url = UPDGET_USERTOKEN_URL;
                    break;
                case 4://封禁網易雲通信ID
                    url = BLOCK_USERID_URL;
                    break;
                case 5://解禁被封禁的網易雲通信ID
                    url = UNBLOCK_USERID_URL;
                    break;
                default:
                    return "{\"desc\":\"方法選擇錯誤!\",\"code\":-1}";
            }
            WebRequest wReq = WebRequest.Create(url);
            //------設定雲信的相關校驗值------
            wReq.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            wReq.Method = "POST";
            wReq.Headers.Add("AppKey", appKey);
            //隨機數(最大長度128個字符)
            string nonce = new Random().Next(100000, 999999).ToString();
            wReq.Headers.Add("Nonce", nonce);
            //當前UTC時間戳,從1970年1月1日0點0 分0 秒開始到現在的秒數(String)
            string curTime = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
            wReq.Headers.Add("CurTime", curTime);
            //SHA1(AppSecret + Nonce + CurTime),三個參數拼接的字符串,進行SHA1哈希計算,轉化成16進制字符(String,小寫)
            wReq.Headers.Add("CheckSum", SHA1_Hash(appSecret + nonce + curTime));
            //------設定雲信的相關校驗設定------

            //-----傳遞相關操作參數-----
            byte[] btBodys = Encoding.UTF8.GetBytes(reqParams);
            wReq.ContentLength = btBodys.Length;
            using (var wsr = wReq.GetRequestStream())
            {
                wsr.Write(btBodys, 0, btBodys.Length);
            }
            //-----傳遞相關操作參數-----

            WebResponse wResp = wReq.GetResponse();
            Stream respStream = wResp.GetResponseStream();

            string resultJson;
            using (StreamReader reader = new StreamReader(respStream,Encoding.UTF8))
            {
                resultJson = reader.ReadToEnd();
            }
            //Json數據:desc描述,code狀態碼
            return resultJson;
        }

        //【工具】計算SHA1值
        private static string SHA1_Hash(string str_sha1_in)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
            str_sha1_out = str_sha1_out.Replace("-", "").ToLower();
            return str_sha1_out;
        }
    }
}

  

網易雲信IM C#.Net請求、操作網易雲通信ID