当前位置: 移动技术网 > IT编程>开发语言>.net > c# 调用微吼直播API

c# 调用微吼直播API

2018年09月18日  | 移动技术网IT编程  | 我要评论

时代周刊中文版,一站通,浪漫星屋

/// <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);
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网