api net调用示例

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using SyncChapterTree.ExternalApi;

namespace SyncChapterTree.test {
    [TestClass]
    public class ApiTester {
        [TestMethod]
        public void TC_SignGenerate() {
            var developerId = "123456789";
            var accessSecret = "123333";

            var paramDic = new Dictionary();
            paramDic.Add("developerId", developerId);
            paramDic.Add("timestamp", "当前时间戳,精确到毫秒");
            paramDic.Add("sid", "303");
            paramDic.Add("vid", "146");
            paramDic.Add("tid", "764");
            paramDic.Add("startPage", "1");
            paramDic.Add("countPerPage", "20");
            paramDic.Add("year", "2017");

            var needEncrptString = new StringBuilder();
            var orderedKey = paramDic.Keys.OrderBy(p => p).ToList();

            for (int i = 0; i < paramDic.Count; i++) {
                var key = orderedKey[i];
                var value = paramDic[key];

                needEncrptString.AppendFormat("{0}={1}&", key, value);
            }

            needEncrptString.AppendFormat("{0}={1}", "accessSecret", accessSecret);

            var buffer = Encoding.UTF8.GetBytes(needEncrptString.ToString());

            var encryptionData = SHA1.Create().ComputeHash(buffer);

            var signature = new StringBuilder();
            foreach (var data in encryptionData) {
                signature.Append(data.ToString("x2"));
            }


            Assert.AreEqual("780b1e4cb8112b871a78716029ae853155195243", signature.ToString());
        }

        [TestMethod]
        public void TC_ZXXKWebApiClient_GetSubjects() {
            var client = new ZXXKWebApiClient();
            var subjects = Task.Run(() => client.GetSubjects(Entities.Stage.Gz)).Result;
            Assert.IsNotNull(subjects);
        }
    }
}

你可能感兴趣的:(api net调用示例)