Ejemplo n.º 1
0
        public Task <JsonRpcResponse> PostTaskAsync(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            var webReq = WebRequest.CreateHttp(uri);

            webReq.Method      = "POST";
            webReq.ContentType = "text/json";

            return(webReq.GetRequestStreamAsync().ContinueWith(t =>
            {
                using (var reqStream = t.Result)
                {
                    this.SerializeTo(reqStream);
                }
            }).ContinueWith(t2 =>
            {
                return webReq.GetReponseAsync().ContinueWith <JsonRpcResponse>(ca2 =>
                {
                    var webRep = ca2.Result;
                    using (var repStream = webRep.GetResponseStream())
                    {
                        var jsonRep = JsonRpcResponse.Deserialize(repStream);
                        return jsonRep;
                    }
                }, TaskContinuationOptions.AttachedToParent).Result;
            }));
        }
Ejemplo n.º 2
0
        private IEnumerator <int> GetPostEnumerator(
            Uri uri, AsyncEnumerator ae, Action <JsonRpcResponse, Exception> resultCallback)
        {
            var webReq = (HttpWebRequest)WebRequest.Create(uri);

            webReq.Method      = "POST";
            webReq.ContentType = "text/json";
            webReq.BeginGetRequestStream(ae.End(), null);

            yield return(1);

            var reqStream = webReq.EndGetRequestStream(ae.DequeueAsyncResult());

            using (var sw = new StreamWriter(reqStream, Encoding.UTF8))
            {
                var json = JsonConvert.SerializeObject(this, Formatting.None);
                sw.Write(json);
                sw.Flush();
            }

            webReq.BeginGetResponse(ae.End(), null);

            yield return(1);

            try
            {
                using (var webRep = webReq.EndGetResponse(ae.DequeueAsyncResult()))
                    using (var repStream = webRep.GetResponseStream())
                    {
                        var jsonRep = JsonRpcResponse.Deserialize(repStream);
                        resultCallback(jsonRep, null);
                    }
            }
            catch (Exception ex)
            {
                resultCallback(null, ex);
                yield break;
            }
        }