HttpWebRequest强制新连接

时间:2015-08-13 22:13:53

标签: c# multithreading httpwebrequest system.net.httpwebrequest

我想使用HttpWebRequest.AddRange(int, int)的多个线程从有限传输服务器下载大文件。问题是我的所有线程都被GetResponse()阻塞,可能是因为他们想要重用第一个线程的连接。我已将请求的KeepAlive属性设置为false,并且我使用ServicePointManager.DefaultConnectionLimit = 1000;

增加了并发连接的最大限制

有趣的是,它有时会起作用。如果我使用2个线程,有时候使用3个线程,并且我从未看到它使用4个或更多线程,它大部分时间都可以工作。

这是我的代码的一部分,删除了非常重要的部分,仍然给出的代码片段是我的代码中的连续块,尽管有评论,但我没有删除任何内容。

for (int i=0; i < threadpool; i++)
{
    pool[i] = new Thread(new ParameterizedThreadStart((object id) =>
    {
        int myId = (int)(id);
        Console.WriteLine("StartThread " + myId);
        byte[] buffer = new byte[chunksize];

        while (currentChunk < chunks)
        {
            int myJob = -1;
            HttpWebResponse response = null;

            lock (currentChunkLock)
            {
                myJob = currentChunk++;
            }

            Console.WriteLine(myId + " GOT JOB " + myJob);
            HttpWebRequest request = MakeRangedRequest(url, myJob * chunksize, chunksize);
            Console.WriteLine(myId + " MADE REQUEST " + myJob);
            response = (HttpWebResponse)request.GetResponse();
            //They get all stuck here
            Console.WriteLine(myId + " GOT RESPONSE " + myJob);

            int totalCount = 0;
            int targetCount = (myJob + 1 == chunks) ? lastChunkSize : chunksize;
            Thread.Sleep(1);

            while (totalCount < targetCount)
            {
                //The only thread that passes is stuck here, it won't allow other threads to continue.
                int left = targetCount-totalCount;
                int count = response.GetResponseStream().Read(buffer, totalCount, left > 1024 ? 1024 : left);
                totalCount += count;
                totalBytesSoFar += count;
                Console.WriteLine("READING " + myId + "/" + totalCount);
                Thread.Sleep(1);
            }
            response.Close();

            Console.WriteLine(myId + " READ BUFFER " + myJob);
            lock (file)
            {
                file.Seek(myJob * chunksize, SeekOrigin.Begin);
                file.Write(buffer, 0, chunksize);
                file.Flush();
            }
           Console.WriteLine(myId + " WRITE FILE " + myJob);

           Console.WriteLine("Current chunk: " + myJob + "/" + chunks + "\r");
           Console.WriteLine("Thread " + myId);
           Thread.Sleep(1);
        }

        Console.WriteLine("Thread " + myId + " out.");
        lock (threadsDonePool)
        {
            threadsDone++;
            if (threadsDone == threadpool)
            {
                file.Close();
                Console.WriteLine("File Closed.");
            }
        }
    }));
    pool[i].Start(i);
}

这是MakeRangedRequest函数:

static HttpWebRequest MakeRangedRequest(string url, int from, int size)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.AddRange(from, from + size);
    request.KeepAlive = false;
    return request;
}

我是否被迫使用TCP类完成所有操作?坚持HttpWebRequest

会很棒

1 个答案:

答案 0 :(得分:0)

您是否尝试过异步操作?

请参考这个: How to use HttpWebRequest (.NET) asynchronously?

希望这有帮助