OpenQA.Selenium.Remote.HttpCommandExecutor.GetTextOfWebResponse C# (CSharp) 메소드

GetTextOfWebResponse() 개인적인 정적인 메소드

private static GetTextOfWebResponse ( HttpWebResponse webResponse ) : string
webResponse System.Net.HttpWebResponse
리턴 string
        private static string GetTextOfWebResponse(HttpWebResponse webResponse)
        {
            // StreamReader.Close also closes the underlying stream.
            Stream responseStream = webResponse.GetResponseStream();
            StreamReader responseStreamReader = new StreamReader(responseStream, Encoding.UTF8);
            string responseString = responseStreamReader.ReadToEnd();
            responseStreamReader.Close();

            // The response string from the Java remote server has trailing null
            // characters. This is due to the fix for issue 288.
            if (responseString.IndexOf('\0') >= 0)
            {
                responseString = responseString.Substring(0, responseString.IndexOf('\0'));
            }

            return responseString;
        }

Usage Example

        private Response CreateResponse(WebRequest request)
        {
            Response        response        = new Response();
            HttpWebResponse httpWebResponse = null;

            try
            {
                httpWebResponse = (request.GetResponse() as HttpWebResponse);
            }
            catch (WebException ex)
            {
                httpWebResponse = (ex.Response as HttpWebResponse);
                if (ex.Status == WebExceptionStatus.Timeout)
                {
                    string format = "The HTTP request to the remote WebDriver server for URL {0} timed out after {1} seconds.";
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, format, new object[]
                    {
                        request.RequestUri.AbsoluteUri,
                        this.serverResponseTimeout.TotalSeconds
                    }), ex);
                }
                if (ex.Response == null)
                {
                    string format2 = "A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL {0}. The status of the exception was {1}, and the message was: {2}";
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, format2, new object[]
                    {
                        request.RequestUri.AbsoluteUri,
                        ex.Status,
                        ex.Message
                    }), ex);
                }
            }
            if (httpWebResponse == null)
            {
                throw new WebDriverException("No response from server for url " + request.RequestUri.AbsoluteUri);
            }
            string textOfWebResponse = HttpCommandExecutor.GetTextOfWebResponse(httpWebResponse);

            if (httpWebResponse.ContentType != null && httpWebResponse.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            {
                response = Response.FromJson(textOfWebResponse);
            }
            else
            {
                response.Value = textOfWebResponse;
            }
            if (this.commandInfoRepository.SpecificationLevel < 1 && (httpWebResponse.StatusCode < HttpStatusCode.OK || httpWebResponse.StatusCode >= HttpStatusCode.BadRequest))
            {
                if (httpWebResponse.StatusCode >= HttpStatusCode.BadRequest && httpWebResponse.StatusCode < HttpStatusCode.InternalServerError)
                {
                    response.Status = WebDriverResult.UnhandledError;
                }
                else if (httpWebResponse.StatusCode >= HttpStatusCode.InternalServerError)
                {
                    if (httpWebResponse.StatusCode == HttpStatusCode.NotImplemented)
                    {
                        response.Status = WebDriverResult.UnknownCommand;
                    }
                    else if (response.Status == WebDriverResult.Success)
                    {
                        response.Status = WebDriverResult.UnhandledError;
                    }
                }
                else
                {
                    response.Status = WebDriverResult.UnhandledError;
                }
            }
            if (response.Value is string)
            {
                response.Value = ((string)response.Value).Replace("\r\n", "\n").Replace("\n", Environment.NewLine);
            }
            httpWebResponse.Close();
            return(response);
        }