示例#1
0
 private static bool Modify(IoT iot, string path)
 {
     try
     {
         string url = "http://" + Settings.Current.Server + path;
         //await Main.GetMainWindow().Dispatcher.BeginInvoke(new Action(delegate () { Message.Show(JsonConvert.SerializeObject(obj), url); }));
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
         request.ContentType     = "application/json";
         request.Method          = "POST";//"PATCH"
         request.CookieContainer = _cookies;
         request.Timeout         = 10000;
         using (var streamWriter = new StreamWriter(request.GetRequestStream()))
         {
             if (iot is Property)
             {
                 streamWriter.Write("{\"value\":" + ((Property)iot).Value + "}");
             }
             if (iot is Script)
             {
                 streamWriter.Write("{\"value\":" + ((Script)iot).Value + "}");
             }
         }
         //Main.GetMainWindow().Dispatcher.BeginInvoke(new Action(delegate () { Message.Show("{\"value\":" + ((Property)obj).value + "}", url); }));
         //Main.GetMainWindow().Dispatcher.BeginInvoke(new Action(delegate () { Message.Show(url, url); }));
         var httpResponse = (HttpWebResponse)request.GetResponseAsync().Result;
         return(httpResponse.StatusCode == HttpStatusCode.OK || httpResponse.StatusCode == HttpStatusCode.Accepted);
         //using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
         //{
         //    var responseText = await streamReader.ReadToEndAsync();
         //    await Main.GetMainWindow().Dispatcher.BeginInvoke(new Action(delegate () { Message.Show(httpResponse.StatusCode.ToString(), ""); }));
         //}
     }
     catch { return(false); }
 }
示例#2
0
            private static IoT CreateIoT(IoT obj, string path)
            {
                try
                {
                    string         url     = "http://" + Settings.Current.Server + path;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.ContentType     = "application/json";
                    request.Method          = "POST";
                    request.CookieContainer = _cookies;
                    request.Timeout         = 10000;
                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        streamWriter.Write(JsonConvert.SerializeObject(obj));
                    }
                    request.GetResponse();

                    url = url + obj.PathUnit;

                    request = (HttpWebRequest)WebRequest.Create(url);
                    request.CookieContainer = _cookies;
                    HttpWebResponse response  = (HttpWebResponse)request.GetResponse();
                    Stream          resStream = response.GetResponseStream();
                    string          resp;
                    using (StreamReader reader = new StreamReader(resStream, Encoding.UTF8))
                    {
                        resp = reader.ReadToEnd();
                    }
                    resp = WebUtility.HtmlDecode(resp);
                    if (string.IsNullOrWhiteSpace(resp))
                    {
                        throw new Exception("Can't get data from server");
                    }
                    return(JsonConvert.DeserializeObject <IoT>(resp));
                }
                catch { return(null); }
            }