Stream data with POST method in WebService

In webservice to recevied from POST method




 // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
public Dictionary GetDataFromStream()
        {
            var request = HttpContext.Current.Request;
            string data = string.Empty;
            try
            {
                if (request.HttpMethod == "POST")
                {
                    using (Stream receiveStream = HttpContext.Current.Request.InputStream)
                    {
                        receiveStream.Position = 0;
                        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                        {
                            data = readStream.ReadToEnd();
                        }
                    }
                }                
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                serializer.MaxJsonLength = Int32.MaxValue;
                Dictionary sData = serializer.Deserialize>(data);
                return sData;
            }
            catch
            { }
            return null;
        }
To call
Dictionary sData = GetDataFromStream();
                string siteCode = sData["siteCode"].ToString();
                string userCode = sData["userCode"].ToString();
public DataPayout GetDataObjectFromStream()
        {
            var request = HttpContext.Current.Request;
            string data = string.Empty;
            DataPayout listData = new DataPayout();
            try
            {
                if (request.HttpMethod == "POST")
                {
                    using (Stream receiveStream = HttpContext.Current.Request.InputStream)
                    {
                        receiveStream.Position = 0;
                        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                        {
                            data = readStream.ReadToEnd();
                        }
                    }
                }               
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                listData = serializer.Deserialize(data);
            }
            catch { }
            return listData;
        }

Reactions

Post a Comment

0 Comments