多线程应用程序中第四个 HttpWebRequest 的奇怪超时

发布于 2024-12-23 07:53:57 字数 1231 浏览 3 评论 0原文

我有一个充当服务器的多线程控制台应用程序。每次新客户端连接到 TcpListener 时,服务器都会生成一个新线程:

//code copied from http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-  threaded-tcp-server
//blocks until a client has connected to the server
TcpClient client = tcpListener.AcceptTcpClient();  
//create a thread to handle communication with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);

该线程使用以下代码发出多个 HttpWebRequest:

public static HttpWebResponse HttpGet(string pRequestURI, string pArgs)
{
    string requestURI = string.Format("{0}?{1}", pRequestURI, pArgs);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURI);
    request.Method = "GET";
    request.ContentType = "application/x-www-form-urlencoded";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    return response;
}

问题是,我在第四个请求上超时。这真的很奇怪,我无法掌握它的窍门。该代码在单线程应用程序中运行得很好。我还确保使用以下命令关闭响应流:

response.Close();

requestURI 是正确的,因为我尝试将其复制并粘贴到浏览器中。实际上,第四个请求是什么并不重要(我尝试过不同的请求),我总是会超时。

我猜这可能与线程限制有关,但我真的不知道如何解决它。任何建议将不胜感激。

谢谢。

I have a multi-threaded console application acting as a server. The server spawns a new thread every time a new client connects to the TcpListener:

//code copied from http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-  threaded-tcp-server
//blocks until a client has connected to the server
TcpClient client = tcpListener.AcceptTcpClient();  
//create a thread to handle communication with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);

The thread makes a number of HttpWebRequests using the following code:

public static HttpWebResponse HttpGet(string pRequestURI, string pArgs)
{
    string requestURI = string.Format("{0}?{1}", pRequestURI, pArgs);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURI);
    request.Method = "GET";
    request.ContentType = "application/x-www-form-urlencoded";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    return response;
}

The problem is that, I get a timeout on the FOURTH REQUEST. It is really weird and I cannot get the hang of it. The code worked just fine when it was in a single-threaded application. I am also making sure to close the response stream using:

response.Close();

The requestURI is correct, 'cause I tried copying and pasting it into my browser. Actually, it doesn't matter what the 4th request is (I've tried with different ones), I always get a timeout.

I guess it may be something related to thread-limits, but I really don't know how to solve it. Any suggestions would be greatly appreciated.

Thank you.

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

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

发布评论

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

评论(2

氛圍 2024-12-30 07:53:57

经过大量的血、汗和泪水,我终于解决了这个问题。

这确实是一个愚蠢的错误,事实证明我在这个地方没有关闭请求

由于某些(未知)原因,这不会影响从 Web 应用程序发出的请求,但从控制台应用程序发出请求时,会遇到超时问题。

谢谢@arx 的帮助 - 非常感谢。

After a lot of blood, sweat and tears, I managed to solve it.

It was a stupid mistake really, turns out there was this one place where I was not closing the request.

For some (unknown) reason, this does not affect the requests when made from a Web Application, but, when issuing the requests from a Console Application, timeout problems are encountered.

Thank you @arx for your help - much appreciated.

娇纵 2024-12-30 07:53:57

request.ServicePoint.CloseConnectionGroup(request.ConnectionGroupName);
这行代码解决了我的问题
谢谢

request.ServicePoint.CloseConnectionGroup(request.ConnectionGroupName);
This line of code resolved my issue
Thanks

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