当前位置: 首页>>代码示例>>C#>>正文


C# HttpClient.GetInputStreamAsync方法代码示例

本文整理汇总了C#中System.Net.Http.HttpClient.GetInputStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.GetInputStreamAsync方法的具体用法?C# HttpClient.GetInputStreamAsync怎么用?C# HttpClient.GetInputStreamAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.HttpClient的用法示例。


在下文中一共展示了HttpClient.GetInputStreamAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OpenConnection

        public async Task<bool> OpenConnection(Uri uri, TimeSpan? timeout = null)
        {
            if (uri == null)
            {
                throw new ArgumentNullException();
            }

            if (state != ConnectionState.Closed)
            {
                return true;
            }

            state = ConnectionState.TryingConnection;

#if WINDOWS_PHONE_APP||WINDOWS_APP||NETFX_CORE
            var filter = new HttpBaseProtocolFilter();
            filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent;

            var httpClient = new HttpClient(filter);
#else
            var httpClient = new HttpClient(new WebRequestHandler
            {
                CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore),
            });
#endif

            var to = (timeout == null) ? TimeSpan.FromMilliseconds(DEFAULT_REQUEST_TIMEOUT) : timeout;
            StartTimer((int)to.Value.TotalMilliseconds, httpClient);

            try
            {
#if WINDOWS_PHONE_APP||WINDOWS_APP||NETFX_CORE
                var str = await httpClient.GetInputStreamAsync(uri);
#else
                var str = await httpClient.GetStreamAsync(uri);
#endif
                state = ConnectionState.Connected;
                var task = Task.Factory.StartNew(() =>
                {
#if WINDOWS_PHONE_APP||WINDOWS_APP||NETFX_CORE
                    using (var core = new StreamAnalizer(str.AsStreamForRead()))
#else
                    using (var core = new StreamAnalizer(str))
#endif
                    {
                        core.RunFpsDetector();
                        core.JpegRetrieved = (packet) => { OnJpegRetrieved(new JpegEventArgs(packet)); };
                        core.PlaybackInfoRetrieved = (packet) => { OnPlaybackInfoRetrieved(new PlaybackInfoEventArgs(packet)); };
                        core.FocusFrameRetrieved = (packet) => { OnFocusFrameRetrieved(new FocusFrameEventArgs(packet)); };

                        while (state == ConnectionState.Connected)
                        {
                            try
                            {
                                core.ReadNextPayload();
                            }
                            catch (Exception e)
                            {
                                Log("Caught " + e.GetType() + ": finish reading loop");
                                break;
                            }
                        }
                    }
                    Log("End of reading loop");
                    OnClosed(null);
                });
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
开发者ID:kazyx,项目名称:kz-image-stream,代码行数:73,代码来源:StreamProcessor.cs


注:本文中的System.Net.Http.HttpClient.GetInputStreamAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。