Ards_v6_Tester.RestClient.DoPost C# (CSharp) Метод

DoPost() публичный статический Метод

public static DoPost ( string requestUrl, string requestBody, string authToken ) : string
requestUrl string
requestBody string
authToken string
Результат string
        public static string DoPost(string requestUrl, string requestBody, string authToken)
        {
            try
            {
                var httpWReq = (HttpWebRequest)WebRequest.Create(requestUrl);

                var encoding = new ASCIIEncoding();
                string postData = requestBody;
                byte[] data = encoding.GetBytes(postData);

                httpWReq.Method = "POST";
                httpWReq.Accept = "application/json";
                httpWReq.ContentType = "application/json";
                httpWReq.ContentLength = data.Length;
                httpWReq.Headers.Add("Authorization", authToken);

                using (Stream stream = httpWReq.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                var response = (HttpWebResponse)httpWReq.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.InternalServerError)
                {
                    string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    try
                    {
                        var x = JToken.Parse(responseString).ToString(Newtonsoft.Json.Formatting.Indented);
                        return x;
                    }
                    catch
                    {
                        return responseString;
                    }
                }
                return string.Empty;

            }
            catch (Exception e)
            {
                return e.Message;
            }
        }