Ejemplo n.º 1
0
        public static void PostJsonAsync(string url, Object data, OnAsyncResponse onResponse)
        {
            string json = JsonConvert.SerializeObject(data);

            byte[] bytes = Encoding.UTF8.GetBytes(json);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method        = "POST";
            request.ContentLength = bytes.Length;
            request.ContentType   = "application/json";

            // start the asynchronous operation
            request.BeginGetRequestStream(new AsyncCallback((result) =>
            {
                HttpWebRequest req = (HttpWebRequest)result.AsyncState;

                Stream postStream = req.EndGetRequestStream(result);
                postStream.Write(bytes, 0, bytes.Length);
                postStream.Close();

                req.BeginGetResponse(new AsyncCallback(responseResult =>
                {
                    HttpWebRequest req1 = (HttpWebRequest)responseResult.AsyncState;

                    if (responseResult.IsCompleted)
                    {
                        var webResponse = req1.EndGetResponse(responseResult);
                        using (var stream = webResponse.GetResponseStream())
                        {
                            using (var read = new StreamReader(stream))
                            {
                                if (onResponse != null)
                                {
                                    onResponse.Invoke(read.ReadToEnd());
                                }
                            }
                        }
                    }
                }), req);
            }), request);
        }
Ejemplo n.º 2
0
        public static void PostJsonAsync(string url, Object data, OnAsyncResponse onResponse)
        {
            string json = JsonConvert.SerializeObject(data);
            byte[] bytes = Encoding.UTF8.GetBytes(json);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentLength = bytes.Length;
            request.ContentType = "application/json";

            // start the asynchronous operation
            request.BeginGetRequestStream(new AsyncCallback((result) =>
            {
                HttpWebRequest req = (HttpWebRequest)result.AsyncState;

                Stream postStream = req.EndGetRequestStream(result);
                postStream.Write(bytes, 0, bytes.Length);
                postStream.Close();

                req.BeginGetResponse(new AsyncCallback(responseResult =>
                {
                    HttpWebRequest req1 = (HttpWebRequest)responseResult.AsyncState;

                    if (responseResult.IsCompleted)
                    {
                        var webResponse = req1.EndGetResponse(responseResult);
                        using (var stream = webResponse.GetResponseStream())
                        {
                            using (var read = new StreamReader(stream))
                            {
                                if (onResponse != null)
                                    onResponse.Invoke(read.ReadToEnd());
                            }
                        }
                    }
                }), req);
            }), request);
        }