/// /// 調用微吼直播API /// /// /// /// 介面地址 /// Json格式的參數(公共參數+介面參數) /// public string GetLiveList(string appKey,string secrectKey,string url,string paramJ... ...
/// <summary> /// 調用微吼直播API /// </summary> /// <param name="appKey"></param> /// <param name="secrectKey"></param> /// <param name="url">介面地址</param> /// <param name="paramJson">Json格式的參數(公共參數+介面參數)</param> /// <returns></returns> public string GetLiveList(string appKey,string secrectKey,string url,string paramJson) { //unix時間戳 DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); int time = (int)(DateTime.Now.AddHours(-8) - dt).TotalSeconds; string Timestamp = time.ToString(); //對參數KEY進行升序排序 paramJson = StortJson(paramJson); //把Json字元串轉換成Dictionary對象 var objJson = JsonConvert.DeserializeObject<Dictionary<string, object>>(paramJson); //簽名字元串 string sign = secrectKey; foreach (var temp in objJson) { sign += temp.Key + temp.Value; } sign += secrectKey; //對簽名字元串進行MD5加密 var signMD5 = FormsAuthentication.HashPasswordForStoringInConfigFile(sign, "MD5"); //把MD5密文轉換成全小寫(加密出來的MD5是大寫,調用微吼API介面需要小寫) signMD5 = signMD5.ToLower(); //把簽名放進Dictionary對象 objJson.Add("sign", signMD5); //請求參數 string completeUrl = string.Empty; foreach (var temp in objJson) { completeUrl += temp.Key + "=" + temp.Value + "&"; } completeUrl = completeUrl.Substring(0, completeUrl.Length - 1); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ProtocolVersion = HttpVersion.Version10; byte[] data = Encoding.UTF8.GetBytes(completeUrl); request.ContentLength = data.Length; Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); HttpWebResponse response = null; int httpStatus = 200; string content; try { response = (HttpWebResponse)request.GetResponse(); httpStatus = (int)response.StatusCode; StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); content = reader.ReadToEnd(); } catch (WebException e) { response = (HttpWebResponse)e.Response; httpStatus = (int)response.StatusCode; using (Stream errData = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(errData)) { content = reader.ReadToEnd(); } } } return content; }
/// <summary> /// 對json字元串進行排序 /// </summary> /// <param name="json"></param> /// <returns></returns> public static string StortJson(string json) { var dic = JsonConvert.DeserializeObject<SortedDictionary<string, object>>(json); SortedDictionary<string, object> keyValues = new SortedDictionary<string, object>(dic); keyValues.OrderBy(m => m.Value);//升序 //keyValues.OrderByDescending(m => m.Key);//降序 return JsonConvert.SerializeObject(keyValues); }