Wednesday 28 December 2011

Get data based on url

Hi,
The post is for the fetching the data by the given url, into it's respective type.
mostly help for fetching the REST or Normal webservice by provoding it's respective url.


        private static object GetDataByRestfulServicePath(string restfulServiceURL)
        {
           restfulServiceURL   =  "http://localhost:52996/Service.svc/Employee";

            using (WebClient webClient = new WebClient())
            {
                //webClient.Credentials = new NetworkCredential("aditya", "mail_123", "star");

                byte[] metaDatainBytes = webClient.DownloadData(new Uri(restfulServiceURL));

                //string str = System.Text.ASCIIEncoding.UTF8.GetString(metaDatainBytes);

                if (metaDatainBytes != null)
                {
                    using (Stream strm = new MemoryStream(metaDatainBytes))
                    {
                        DataContractSerializer obj = new DataContractSerializer(typeof(object));
                        return (object)obj.ReadObject(strm);
                    }
                }
            }

            return null;
        }

The "object" should be replaced by the respective type. for ex: string if, in the interface, the return type is string.

No comments:

Post a Comment