Exemplo n.º 1
0
        /// <summary>
        /// Js equivalent: Ajax.
        /// We use HttpClient, see https://stackoverflow.com/a/4015346/7362888
        /// </summary>
        /// <param name="url"></param>
        /// <param name="req"></param>
        /// <param name="onResp"></param>
        internal async Task <AnsonMsg> Post(string url, AnsonMsg req)
        {
            using (var client = new HttpClient())
            {
                try
                {
                    // what about http stream?
                    MemoryStream s = new MemoryStream();
                    req.ToBlock(s);
                    StringContent payload = new StringContent(anson.Utils.ToString(s),
                                                              Encoding.UTF8, "application/json");
                    HttpResponseMessage jresponse = await client.PostAsync(url, payload).ConfigureAwait(false);

                    AnsonMsg resp = (AnsonMsg)Anson.FromJson(await jresponse.Content.ReadAsStringAsync());

                    // onResp(resp.code, resp);
                    return(resp);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.StackTrace);
                    // throw new IOException(ex.Message);
                    return(new AnsonMsg(req.port, new MsgCode(MsgCode.exIo))
                           .Body(new AnsonResp(null, ex.Message), null));
                }
            }
        }
Exemplo n.º 2
0
        public void TestSessionReq()
        {
            // formatLogin: {a: "login", logid: logId, pswd: tokenB64, iv: ivB64};
            AnsonMsg reqv11 = AnSessionReq.formatLogin(uid, tk64, iv64);

            MemoryStream stream = new MemoryStream();

            reqv11.ToBlock(stream);
            string json = Utils.ToString(stream);

            // json:
            // {type: io.odysz.anson.jprotocol.AnsonMsg,
            //  code: null, opts: null,
            //  port: "session",
            //  header: null, vestion: "1.0",
            //  body: [{type: io.odysz.anson.jprotocol.AnSessionReq,
            //          uid: "test-id",
            //          parent: "io.odysz.anson.jprotocol.AnsonMsg",
            //          a: "login", conn: null,
            //          iv: "iv: I'm base64", mds: null,
            //          token: "tk: I'm base64"}], seq: 909}
            AnsonMsg msg = (AnsonMsg)Anson.FromJson(json);

            Assert.AreEqual(reqv11.code, msg.code);
            Assert.AreEqual(reqv11.port.name, msg.port.name);
            Assert.AreEqual(((AnSessionReq)reqv11.Body(0)).iv, ((AnSessionReq)msg.Body(0)).iv);
            Assert.AreEqual(((AnSessionReq)reqv11.Body(0)).token, ((AnSessionReq)msg.Body(0)).token);
        }
Exemplo n.º 3
0
        public void AnstonTest()
        {
            Anson  a   = new();
            string jsn = JsonConvert.SerializeObject(a);

            StringAssert.Contains(jsn, "{}");
            Anson b = JsonConvert.DeserializeObject <Anson>(jsn);

            Console.WriteLine(b);
        }
Exemplo n.º 4
0
        public void TestAnsonResp()
        {
            AnsonMsg respmsg = (AnsonMsg)Anson.FromJson(respjson);

            Assert.AreEqual("ok", respmsg.code.Name());
            Assert.AreEqual(Port.update, respmsg.port.port());

            AnsonResp resp = (AnsonResp)respmsg.Body(0);

            Assert.IsNotNull(resp);
            Assert.IsNotNull(resp.map);
            SemanticObject resulved = (SemanticObject)resp.map["resulved"];

            Assert.IsNotNull(resulved);

            IDictionary    props  = resulved.props;
            SemanticObject attach = (SemanticObject)props["a_attaches"];

            Assert.IsNotNull(attach.props);
            Assert.AreEqual("00000D", attach.props["attId"]);
        }
Exemplo n.º 5
0
        public string streamdown(string url, AnsonMsg jreq, string localpath)
        {
            HttpWebRequest  req = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse con = (HttpWebResponse)req.GetResponse();

            //add reuqest header
            req.Method    = "POST";
            req.UserAgent = USER_AGENT;
            // req.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            req.Headers.Add("Accept-Language", "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3");
            // con.setRequestProperty("Content-Type", "text/plain");
            req.ContentType = "text/plain";
            // con.setRequestProperty("charset", "utf-8");
            req.TransferEncoding = "utf-8";

            // Send post request
            // con.setDoOutput(true);
            using (Stream stream = req.GetResponse().GetResponseStream())
            {
                jreq.ToBlock(stream);
            }

            if (AnClient.verbose)
            {
                Utils.Logi(url);
            }

            Stream     ins = con.GetResponseStream();
            FileStream ofs = new FileStream(localpath, FileMode.OpenOrCreate);

            ins.CopyTo(ofs);
            ofs.Close();

            AnsonMsg s    = null;
            string   type = null;

            try {
                // FileInputStream ifs = new FileInputStream(localpath);
                // type = detector.detect(ifs);
                // ifs.close();
                if (localpath.EndsWith(".json"))
                {
                    type = "json";
                }
            }
            catch (Exception e) {
                return(localpath);
            }

            if (type != null && type.StartsWith("json"))
            {
                FileStream ifs = new FileStream(localpath, FileMode.OpenOrCreate);
                try
                {
                    s = (AnsonMsg)Anson.FromJson(ifs);
                }
                catch (Exception e)
                {
                    return(localpath);
                }
                finally { ifs.Close(); }
                throw new SemanticException("Code: %s\nmsg: %s", s.code, ((AnsonResp)s.Body(0)).Msg());
            }

            return(localpath);
        }