Wednesday, March 30, 2016

C# call web service using rest

Uri address = new Uri("http://test.tst.info/ecabinetapi/eCabinetAPI.asmx/generateeCabinetConfig");

                HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                StringBuilder data = new StringBuilder();
                byte[] byteData = UTF8Encoding.UTF8.GetBytes(Convert.ToString(data));
                request.ContentLength = byteData.Length;
                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(byteData, 0, byteData.Length);
                }

                request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                string result = string.Empty;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    result = reader.ReadToEnd();
                }

No comments:

Post a Comment