Argentini.Halide.H3HttpREST.Call C# (CSharp) Method

Call() public method

Execute the REST request.
public Call ( ) : string
return string
        public string Call()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            HttpWebResponse response = null;
            string result = "";

            request.Method = RequestType.ToString();
            request.AllowAutoRedirect = true;
            request.UserAgent = "Mozilla/5.0";
            request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
            request.CookieContainer = new CookieContainer();
            request.Referer = "";

            switch (RequestFormat)
            {
                case RequestFormatEnum.JSON:
                    //request.ContentType = "application/json; charset=utf-8";
                    request.Accept = "application/json";
                    break;

                case RequestFormatEnum.XML:
                    //request.ContentType = "application/xml; charset=utf-8";
                    request.Accept = "application/xml";
                    break;
            }

            request.Timeout = Timeout;
            request.ReadWriteTimeout = Timeout;

            if (HeaderItems != null)
            {
                foreach (KeyValuePair<string, string> item in HeaderItems)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }

            if (RequestType == RequestTypeEnum.POST || RequestType == RequestTypeEnum.PUT || RequestType == RequestTypeEnum.PATCH)
            {
                if (String.IsNullOrEmpty(Payload))
                {
                    Payload = "";
                }

                var dataToSend = Encoding.UTF8.GetBytes(Payload);

                request.ContentLength = dataToSend.Length;

                request.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
            }

            try
            {
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            result = readStream.ReadToEnd();
                        }
                    }

                    if (response != null)
                    {
                        LastStatus = ((Int32)response.StatusCode).ToString();
                    }
                }
            }

            catch (WebException ex)
            {
                LastStatus = result = ex.ToString();
            }

            return result;
        }

Usage Example

Beispiel #1
0
        public void TestREST()
        {
            H3HttpREST restRequest = new H3HttpREST();

            restRequest.RequestType = H3HttpREST.RequestTypeEnum.GET;
            restRequest.RequestFormat = H3HttpREST.RequestFormatEnum.JSON;
            restRequest.Timeout = 30000;
            restRequest.Url = "https://jsonplaceholder.typicode.com/posts/1";
            restRequest.HeaderItems.Add("X-App-Token", "test-token-value");

            string fullResult = restRequest.Call();

            Console.WriteLine(fullResult);

            restRequest.RequestType = H3HttpREST.RequestTypeEnum.POST;
            restRequest.Payload = "{ \"title\": \"this is the POSTED title\", \"body\": \"this is the POSTED body\" }";
            restRequest.Url = "https://jsonplaceholder.typicode.com/posts";

            fullResult = restRequest.Call();

            Console.WriteLine(fullResult);

            restRequest.RequestType = H3HttpREST.RequestTypeEnum.POST;
            restRequest.Payload = "";
            restRequest.Url = "https://jsonplaceholder.typicode.com/posts";

            fullResult = restRequest.Call();

            Console.WriteLine(fullResult);

            restRequest.RequestType = H3HttpREST.RequestTypeEnum.PUT;
            restRequest.Payload = "{ \"title\": \"this is the PUT title\", \"body\": \"this is the PUT body\" }";
            restRequest.Url = "https://jsonplaceholder.typicode.com/posts/1";

            fullResult = restRequest.Call();

            Console.WriteLine(fullResult);
        }