System.Net.WebRequest.GetResponseAsync C# (CSharp) Method

GetResponseAsync() public method

public GetResponseAsync ( ) : System.Threading.Tasks.Task
return System.Threading.Tasks.Task
        public virtual System.Threading.Tasks.Task<System.Net.WebResponse> GetResponseAsync() { throw null; }        
        public static System.Net.IWebProxy GetSystemWebProxy() { throw null; }

Same methods

WebRequest::GetResponseAsync ( ) : Task

Usage Example

        private async Task<JObject> _ReadResponseAsync(WebRequest request, string inputAddress)
        {
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)(await request.GetResponseAsync().ConfigureAwait(false));
            }
            catch (WebException we)
            {
                response = (HttpWebResponse)we.Response;
            }

            JObject responseJson;
            using (var responseStream = new StreamReader(response.GetResponseStream()))
            {
                var responseStr = await responseStream.ReadToEndAsync().ConfigureAwait(false);
                responseJson = JObject.Parse(responseStr); // I'm fine throwing a parse error here.
            }

            if (response.StatusCode == HttpStatusCode.OK) // Could probably relax this to "non-failing" codes.
            {
                return responseJson;
            }
            else
            {
                throw new Exception(string.Format("Failed call: {0} failed to OCR - code {1} - details\n{2}", 
                    inputAddress, response.StatusCode, responseJson.ToString(Newtonsoft.Json.Formatting.Indented)));
            }
        }
All Usage Examples Of System.Net.WebRequest::GetResponseAsync