コード例 #1
0
ファイル: MediaClient.cs プロジェクト: keepingcode/paper
        public async Task <Ret <Result> > TransferAsync(
            UriString route,
            string method      = MethodNames.Get,
            Entity requestData = null)
        {
            try
            {
                Ret ret = null;
                await DoRequestAsync(route, method, null, requestData, webResponse =>
                {
                    var isRedirect = ((int)webResponse.StatusCode / 100) == 3;
                    if (isRedirect)
                    {
                        var location = webResponse.Headers[HttpResponseHeader.Location];
                        var uri      = new Uri(location, UriKind.RelativeOrAbsolute);
                        ret          = Ret.Ok(uri, webResponse.StatusCode);
                    }
                    else
                    {
                        using (var stream = webResponse.GetResponseStream())
                        {
                            var entity = EntityParser.ParseEntity(stream);
                            ret        = Ret.Ok(entity, webResponse.StatusCode);
                        }
                    }
                });

                return(ret);
            }
            catch (Exception ex)
            {
                ex.Trace();
                return(ex);
            }
        }
コード例 #2
0
ファイル: MediaClient.cs プロジェクト: keepingcode/paper
        public async Task <Ret> CheckConnectivityAsync()
        {
            try
            {
                var uri    = new UriString(Settings.Endpoint).Combine(ApiInfo.Prefix, "/Status");
                var client = WebRequest.CreateHttp(uri.ToString());
                using (var response = await client.GetResponseAsync())
                    using (var stream = response.GetResponseStream())
                    {
                        try
                        {
                            // Checando se a resposta é uma entidade (Entity) válida.
                            EntityParser.ParseEntity(stream);

                            // TODO: Futuramente a entidade pode ser investigada para
                            // determinar se é uma entidade compatível com a esperada
                            // para a consulta de status.

                            return(Ret.Ok());
                        }
                        catch (Exception ex)
                        {
                            ex.Trace();
                            return(Ret.Ok(HttpStatusCode.UnsupportedMediaType));
                        }
                    }
            }
            catch (Exception ex)
            {
                return(ex);
            }
        }