zoukankan      html  css  js  c++  java
  • c# 调用微吼直播API

    /// <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);
    }
  • 相关阅读:
    HihoCoder 1638 : 小Hi的天平 (2-sat+并查集)
    阿里云安全肖力:云上数据安全体系建设的六要素
    MaxCompute客户端(odpscmd)在windows命令行下查询中文乱码问题处理实践
    序列化方案选型对比
    亚洲唯一,阿里云SLB位列Gartner全球网络负载均衡市场前五
    阿里云OSS同城冗余存储技术解析
    OSS跨同城3AZ重磅发布,构造全面数据保护体系
    阿里云OSS同城冗余存储正式商业化,提供云上同城容灾能力
    云原生应用 Kubernetes 监控与弹性实践
    GIAC2019 演讲精选 | 面向未来的黑科技——UI2CODE闲鱼基于图片生成跨端代码
  • 原文地址:https://www.cnblogs.com/cc-net/p/9661379.html
Copyright © 2011-2022 走看看