WebRequest 线程在两个请求处阻塞

发布于 2024-11-30 13:53:57 字数 1722 浏览 0 评论 0原文

我需要测试数据上下文并查看它在多个同时请求下的行为,为此我制作了一个简单的控制台应用程序,[理论上]将发送这些请求:

private static DateTime startTime = DateTime.Now.AddSeconds(5);
public static Random rand = new Random();

static void Main(string[] args)
{
    const byte testThreads = 10;

    ThreadStart[] threadStarts = new ThreadStart[testThreads];
    Thread[] threads = new Thread[testThreads];

    for (byte i = 0; i < testThreads; i++)
    {
        threadStarts[i] = new ThreadStart(ExecutePOST);
        threads[i] = new Thread(threadStarts[i]);
    }

    for (byte i = 0; i < testThreads; i++){ threads[i].Start(); }

    for (byte i = 0; i < testThreads; i++){ threads[i].Join(); }
}

被调用的函数是

private static void ExecutePOST()
{
    while (DateTime.Now < startTime) { }

    Console.WriteLine("{0} STARTING TEST", DateTime.Now.Millisecond);
    WebRequest webRequest = WebRequest.Create(/*URL*/);
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.Method = "POST";

    string name = string.Format("Test {0}", Program.rand.Next(1000));

    byte[] bytes = Encoding.ASCII.GetBytes(/*PARAMETERS*/);
    Stream output = null;
    try
    {
        webRequest.ContentLength = bytes.Length;
        output = webRequest.GetRequestStream();
        output.Write(bytes, 0, bytes.Length);

        Console.WriteLine("{0}:{1}", DateTime.Now.Millisecond, name);
    }
    catch (WebException ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        if (output != null)
        {
            output.Close();
        }
    }
}

我得到的输出是: Output

谁能解释一下这种行为吗?为什么两次请求后就停止了?

谢谢

I need to test a Data Context and see what behavior it has under multiple simultaneous requests, for that I made a simple console application that [in theory] would send these requests:

private static DateTime startTime = DateTime.Now.AddSeconds(5);
public static Random rand = new Random();

static void Main(string[] args)
{
    const byte testThreads = 10;

    ThreadStart[] threadStarts = new ThreadStart[testThreads];
    Thread[] threads = new Thread[testThreads];

    for (byte i = 0; i < testThreads; i++)
    {
        threadStarts[i] = new ThreadStart(ExecutePOST);
        threads[i] = new Thread(threadStarts[i]);
    }

    for (byte i = 0; i < testThreads; i++){ threads[i].Start(); }

    for (byte i = 0; i < testThreads; i++){ threads[i].Join(); }
}

The called function is

private static void ExecutePOST()
{
    while (DateTime.Now < startTime) { }

    Console.WriteLine("{0} STARTING TEST", DateTime.Now.Millisecond);
    WebRequest webRequest = WebRequest.Create(/*URL*/);
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.Method = "POST";

    string name = string.Format("Test {0}", Program.rand.Next(1000));

    byte[] bytes = Encoding.ASCII.GetBytes(/*PARAMETERS*/);
    Stream output = null;
    try
    {
        webRequest.ContentLength = bytes.Length;
        output = webRequest.GetRequestStream();
        output.Write(bytes, 0, bytes.Length);

        Console.WriteLine("{0}:{1}", DateTime.Now.Millisecond, name);
    }
    catch (WebException ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        if (output != null)
        {
            output.Close();
        }
    }
}

The output I get is:
Output

Can anyone please explain this behavior? Why is it stopping after two requests?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2024-12-07 13:53:57

是的,这是因为每个 URL 的连接数默认限制为 2 - 连接被池化。

您通过将数据写入请求流来占用连接,但从未获得响应。一个简单的:

using (webRequest.GetResponse()) {}

在方法结束时很可能会解决它。这将完成请求并释放连接以供另一个请求使用。

另请注意,输出流的 using 语句也会使您的代码更简单。

Yes, this is because the number of connections per URL is limited to 2 by default - the connections are pooled.

You're hogging the connection by writing data to the request stream, but then never getting the response. A simple:

using (webRequest.GetResponse()) {}

at the end of the method is likely to sort it out. That will finish the request and release the connection to be used by another request.

Also note that a using statement for the output stream would make your code simpler too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文