HttpClient ReadAsync永远不会返回

时间:2013-11-16 07:55:31

标签: c# asynchronous async-await

我随机问了我的代码。我已经列出了10 000个用于测试的网址并且随机地从未完成,消息“FINISH”没有打印而没有任何错误/异常。我正在解决这个问题2天没有解决方案,有什么建议吗?

重新定位问题的简短代码: 用于测试https://www.dropbox.com/s/0x6w1ej0fuxedw9/url.txt

的urlist
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProblemApp
{
    static class AsyncForEach
    {
        public static Task ForEachAsync<T>(this IEnumerable<T> source, int dop, Func<T, Task> body)
        {
            return Task.WhenAll(
                from partition in Partitioner.Create(source).GetPartitions(dop)
                select Task.Run(async delegate
                {
                    using (partition)
                        while (partition.MoveNext())
                            await body(partition.Current);
                }));
        }
    }

    public class Form1 : Form
    {
        public Form1()
        {
            Load += OnLoad;
        }

        private void OnLoad(object sender, EventArgs eventArgs)
        {
            Go();
        }

        private async Task<bool> Open(string openurl)
        {

            Uri uri;
            if (!Uri.TryCreate(openurl, UriKind.Absolute, out uri)) return false;

            try
            {
                var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(100));

                using (var httpClient = new HttpClient())
                {
                    using (var req = new HttpRequestMessage(HttpMethod.Get, new Uri(openurl)))
                    {
                        using (
                            HttpResponseMessage response =
                                await
                                    httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead,
                                        timeout.Token).ConfigureAwait(false))
                        {
                            if (response != null &&
                                (response.StatusCode == HttpStatusCode.OK ||
                                 response.StatusCode == HttpStatusCode.NotFound))
                            {
                                using (
                                    Stream responseStream =
                                        await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                                {
                                    int read;
                                    int offset = 0;
                                    var rawResponse = new byte[8192];
                                    while (
                                        (read =
                                            await
                                                responseStream.ReadAsync(rawResponse, 0, 8192, timeout.Token)
                                                    .ConfigureAwait(false)) != 0)
                                    {
                                        offset += read;

                                        if (offset > 1024000)
                                        {
                                            return false;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
            //    Console.WriteLine(ex.Message);
            } catch (HttpRequestException ex)
            {
             //   Console.WriteLine(ex.Message);
            }
            return true;
        }


        private async void Go()
        {
            IEnumerable<string> lines = File.ReadLines("url.txt");

            await
                lines.ForEachAsync(500,
                    async line =>
                    {
                        bool result = await Open(line).ConfigureAwait(false); 
                    });

            Console.WriteLine("FINISH");
        }

    }

}

上一个示例中的ForEachAsync:http://blogs.msdn.com/b/pfxteam/archive/2012/03/05/10278165.aspx

使用HttpCompletionOption.ResponseContentRead程序正常运行

0 个答案:

没有答案