Beispiel #1
0
        public static void Test(string code = "DateTime.Now")
        {
            try
            {
                var res = "oops";
                Thread.Sleep(1000);
                if (AppContext.Config.GetParameter("wcfconsoleworker", "webmode", false))
                {
                    var jss = new Json.JsonSerializer();
                    Func <string, string> get = x => { if (!x.StartsWith("<"))
                                                       {
                                                           return(jss.ReadObject <string>(x));
                                                       }
                                                       else
                                                       {
                                                           var r = new XmlTextReader(new StringReader(x)); while (r.NodeType != XmlNodeType.Text)
                                                           {
                                                               r.Read();
                                                           }
                                                           return(r.Value);
                                                       } };

                    var v = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://localhost:8764/console/Eval");
                    v.Method      = "POST";
                    v.ContentType = "application/json";
                    using (var s = v.GetRequestStream()) jss.ToStream(code).CopyTo(s);
                    res = get(v.GetResponse().GetResponseStream().ReadToEnd().GetString());

                    res = get(new CommonUtils.Telecom.Http.HttpClientEx().PostRaw("http://localhost:8764/console/Eval", jss.ToStream(code)).ReadToEnd().GetString());
                    /*если ResponseFormat = WebMessageFormat.Json) то можно использовать типизированные вариант*/
                    //res = new CommonUtils.Telecom.Http.HttpClientEx().Post("http://localhost:8764/console/Eval", code);
                }
                else
                {
                    var factory = new ChannelFactory <IEvalConsoleService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8764/console"));
                    var proxy   = factory.CreateChannel();
                    res = proxy.Eval(code);
                    ((IClientChannel)proxy).Close();
                    factory.Close();
                }
                Console.WriteLine(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #2
0
        public async Task <Tout> DoRestAsync <Tin, Tout>(System.Net.Http.HttpMethod method, string url, Tin body = default(Tin))
        {
            var client = new HttpClient();

            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            var req = new HttpRequestMessage(method, new Uri(url));
            var jss = new Json.JsonSerializer();

            if (body != null)
            {
                req.Content = new StringContent(jss.ToJson(body), Encoding.UTF8, "application/json");
            }
            var resp = await client.SendAsync(req);

            return(jss.ReadObject <Tout>(await resp.Content.ReadAsStringAsync()));
        }