欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 调用微吼直播API

程序员文章站 2022-12-25 15:30:18
/// /// 调用微吼直播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);
}