示例#1
0
        /// <summary>
        /// 公共请求
        /// </summary>
        /// <param name="wtype">请求操作的类型(值范围在:1-18)具体请求对应值请看对应请求对应值</param>
        /// <param name="body">提交数据</param>
        /// <returns>内容</returns>
        private async static Task <string> Request(int wtype, string body)
        {
            try
            {
                var timestamp = FreeYunUtil.ToTimeStamp(DateTime.Now);


                var tem = FreeYunUtil.DesEncryptToByte(body, desKey, CipherMode.ECB, PaddingMode.PKCS7);
                var hex = FreeYunUtil.ByteToHex(tem);
                //var url = "https://api.freeyun.net/webgateway.html";

                var url = "https://bgp.freeyun.net/webgateway.html";

                var _tstr    = string.Format("{0}{1}{2}{3}{4}", wtype, timestamp, salt, appid, hex);
                var sign     = FreeYunUtil.MD5(_tstr);                                              //version=1.1.3&
                var sendBody = "appid={0}&secretkey={1}&wtype={2}&sign={3}&timestamp={4}&data={5}"; //这个版本号 是sdk网络验证的版本号 非软件的
                sendBody = string.Format(sendBody, appid, secretkey, wtype, sign, timestamp, hex);

                var item = new HttpItem()
                {
                    URL         = url,
                    Method      = System.Net.Http.HttpMethod.Post,
                    Postdata    = sendBody,
                    Encoding    = Encoding.UTF8,
                    ContentType = "application/x-www-form-urlencoded",
                    Timeout     = 30,
                };

                item.Header.Add("Version", "1.1.3");

                var result = await item.GetHtml();

                string html = result.Html;

                /*
                 *
                 *  var buffer = Encoding.UTF8.GetBytes(sendBody);
                 * retry:
                 * var req = HttpWebRequest.Create(url);
                 * req.Timeout = 60 * 1000;
                 * req.Method = "POST";
                 * req.ContentType = "application/x-www-form-urlencoded";
                 * req.ContentLength = buffer.Length;
                 * req.Headers.Add("Version", "1.1.3");
                 *
                 * req.GetRequestStream().Write(buffer, 0, buffer.Length);
                 * var html = string.Empty;
                 * using (var rsp = req.GetResponse())
                 * {
                 *  using (var reader = new StreamReader(rsp.GetResponseStream()))
                 *  {
                 *      html = reader.ReadToEnd();
                 *  }
                 * }
                 * if (html == "")
                 * {
                 *  Thread.Sleep(1000);
                 *  goto retry;
                 * }
                 *
                 */



                JObject json   = (JObject)JsonConvert.DeserializeObject(html);
                var     msg    = json["msg"].ToString();
                var     data   = json["data"].ToString();
                var     status = json["status"].ToString();
                if (status == "-1")
                {
                    throw new Exception(FreeYunUtil.IsNull(msg) ? "未知错误" : msg);
                }


                string str = Encoding.UTF8.GetString(FreeYunUtil.DesDecrypt(FreeYunUtil.HexToByte(data), desKey, CipherMode.ECB, PaddingMode.PKCS7));

                return(str);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return("");
            }

            // body = "{\"version\":\"1.1.1.0\",\"timestamp\":1501150648401}";
        }