HttpWebRequest强制新连接

本文关键字:连接 新连接 HttpWebRequest | 更新日期: 2023-09-27 18:07:47

我想使用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

HttpWebRequest强制新连接

你曾经尝试过异步操作吗?

请参考这个:如何使用HttpWebRequest (.NET)异步?

希望能有所帮助