COOkie设置及控制器中使用

  • Post author:
  • Post category:其他


 <script type="text/javascript">
       $(function () {
           var GetMQtt = getCookie("MQTT").split(',');
           $("#IPConfig").val(GetMQtt[0]);
           $("#Prot").val(GetMQtt[1]);
           $("#ClientID").val(GetMQtt[2]);
           $("#User").val(GetMQtt[3]);
           $("#Pwd").val(GetMQtt[4]);
           ///保存数据
           $("#left").click(function () {
               var Ip = $("#IPConfig").val();
               var Prot = $("#Prot").val();
               var ClientID = $("#ClientID").val();
               var User = $("#User").val();
               var Pwd = $("#Pwd").val();
               var MQTTstr = Ip + "," + Prot + "," + ClientID + "," + User + "," + Pwd
               var dates = 15;
               setCookie("MQTT", MQTTstr,45)
           })
       })

       function setCookie(cname, cvalue, exdays) {
           var d = new Date();
           d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
           var expires = "expires=" + d.toGMTString();
           document.cookie = cname + "=" + cvalue + "; " + expires;
       }
       function getCookie(cname) {
           var name = cname + "=";
           var ca = document.cookie.split(';');
           for (var i = 0; i < ca.length; i++) {
               var c = ca[i].trim();
               if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); }
           }
           return "";
       }
       function checkCookie() {
           var user = getCookie("username");
           if (user != "") {
               alert("欢迎 " + user + " 再次访问");
           }
           else {
               user = prompt("请输入你的名字:", "");
               if (user != "" && user != null) {
                   setCookie("username", user, 30);
               }
           }
       }



   </script>

 public ActionResult SubscribeSend(string send, string Subscribe) {
            bool IsTrue = true;
            string Msg = null;
          
            try
            {


                HttpCookie cookie = HttpContext.Request.Cookies["MQTT"];
                string[] MQTT = cookie.Value.ToString().Split(',');
                string Server = MQTT[0].ToString();
                string Prot = MQTT[1].ToString();
                string ClientID = MQTT[2].ToString();
                string User = MQTT[3].ToString();
                string Pwd = MQTT[4].ToString();
                ClientID = Guid.NewGuid().ToString();
                MQTTFunction.MqttLinkServer(Server, Prot, ClientID, User, Pwd);

                MQTTFunction.MqttSend(Subscribe, ClientID, send);

               
                


            }
            catch (Exception ex)
            {

                Msg = ex.ToString();



            }

            return Json(new { IsTrue = IsTrue, Msg = Msg });

        }



版权声明:本文为qq_42899641原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。