Пример #1
0
        public async Task <string> DoRequestAsync(string url, Dictionary <string, string> specificHeaders,
                                                  Dictionary <string, object> bodyData)
        {
            string         data    = GetRequestPostData(bodyData, specificHeaders);
            var            headers = GetRequestHeaders(specificHeaders);
            HttpWebRequest request = CreateRequest(url, headers, data);

            DebugLog("Request Method: " + request.Method);
            DebugLog("Request URI: " + request.RequestUri);
            DebugLogHeaders(request.Headers, "Request");

            HttpWebResponse response;

            try
            {
                if (!String.IsNullOrEmpty(data))
                {
                    DebugLog("Request Body: " + data);
                    await SendDataAsync(request, data);
                }
                using (response = await request.GetResponseAsync() as HttpWebResponse)
                {
                    DebugLog("Response Status Code: " + response.StatusCode);
                    DebugLog("Response Status Description: " + response.StatusDescription);
                    DebugLogHeaders(response.Headers, "Response");

                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.OK:
                        using (var stream = new StreamReader(response.GetResponseStream()))
                        {
                            return(await stream.ReadToEndAsync());
                        }

                    case HttpStatusCode.NoContent:
                        return(string.Empty);

                    default:
                        throw new OpenTokWebException("Response returned with unexpected status code " +
                                                      response.StatusCode.ToString());
                    }
                }
            }
            catch (WebException e)
            {
                DebugLog("WebException Status: " + e.Status + ", Message: " + e.Message);

                response = (HttpWebResponse)e.Response;

                if (response != null)
                {
                    DebugLog("Response Status Code: " + response.StatusCode);
                    DebugLog("Response Status Description: " + response.StatusDescription);
                    DebugLogHeaders(response.Headers, "Response");

                    if (this.debug)
                    {
                        using (var stream = new StreamReader(response.GetResponseStream()))
                        {
                            DebugLog("Response Body: " + stream.ReadToEnd());
                        }
                    }
                }

                OpenTokUtils.ValidateTlsVersion(e);

                throw new OpenTokWebException("Error with request submission", e);
            }
        }