Arango.ConsoleTests.Performance.SimpleHttpPostCreateDocument C# (CSharp) Method

SimpleHttpPostCreateDocument() public method

public SimpleHttpPostCreateDocument ( string uriString, string body, string username = "test", string password = "test" ) : string
uriString string
body string
username string
password string
return string
        public string SimpleHttpPostCreateDocument(string uriString, string body, string username = "test", string password = "test")
        {
            //var stopwatch = Stopwatch.StartNew();
            int statusCode = 0;
            WebHeaderCollection headers = null;

            var responseBody = "";
            //var httpRequest = HttpWebRequest.CreateHttp(uri);
            var httpRequest = HttpWebRequest.CreateHttp(new Uri(uriString));

            httpRequest.KeepAlive = true;
            httpRequest.SendChunked = false;
            httpRequest.Proxy = null;
            httpRequest.Method = "POST";
            httpRequest.UserAgent = ASettings.DriverName + "/" + ASettings.DriverVersion;

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                httpRequest.Headers.Add(
                    "Authorization",
                    "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password))
                );
            }

            if (!string.IsNullOrEmpty(body))
            {
                httpRequest.ContentType = "application/json; charset=utf-8";

                var data = Encoding.UTF8.GetBytes(body);

                using (var stream = httpRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                    stream.Flush();
                    stream.Close();
                }
            }
            else
            {
                httpRequest.ContentLength = 0;
            }

            try
            {
                using (var httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                using (var responseStream = httpResponse.GetResponseStream())
                using (var reader = new StreamReader(responseStream))
                {
                    statusCode = (int)httpResponse.StatusCode;
                    headers = httpResponse.Headers;
                    responseBody = reader.ReadToEnd();

                    reader.Close();
                    responseStream.Close();
                }
            }
            catch (WebException webException)
            {
                if ((webException.Status == WebExceptionStatus.ProtocolError) &&
                    (webException.Response != null))
                {
                    using (var exceptionHttpResponse = (HttpWebResponse)webException.Response)
                    {
                        if (exceptionHttpResponse.ContentLength > 0)
                        {
                            using (var exceptionResponseStream = exceptionHttpResponse.GetResponseStream())
                            using (var exceptionReader = new StreamReader(exceptionResponseStream))
                            {
                                responseBody = exceptionReader.ReadToEnd();

                                exceptionReader.Close();
                                exceptionResponseStream.Close();
                            }
                        }
                    }
                }
                else
                {
                    throw;
                }
            }

            //Console.WriteLine("{0}", stopwatch.Elapsed.TotalMilliseconds);

            return responseBody;
        }