HttpWebRequests 后续调用失败

发布于 2024-07-11 19:46:17 字数 357 浏览 7 评论 0原文

我知道这是一个模糊的问题,特别是因为我没有提供任何代码,但我正在开发一个 .Net 2.0 应用程序,并且我们有一个 WebRequest 将数据发布到内部构建的 API。

奇怪的事情发生在我们的第三个(并且总是第三个)后续请求上,该请求在请求的 GetRequestStream() 方法处失败。 第一次和第二次调用时,一切都很好。 第三次时,它会挂起一段时间并最终超时。

该 API 正在被内部其他应用程序调用,因此我们知道这不是服务器端或网络问题。 我们已经在几台机器上进行了尝试 - 所有这些机器都有相同的问题。 有没有人以前遇到过这个问题,或者有没有人对如何调试有任何建议(因为响应对象不会产生任何东西,或者至少没有任何有用的东西)。

I know this is a vague question, especially since I am not providing any code, but I am developing a .Net 2.0 application, and we have a WebRequest which posts data to an internally built API.

The strange thing happens on our 3rd (and always the 3rd) subsequent request which fails at the GetRequestStream() method of the request. The first and second time its called, all is fine. On the 3rd time, it hangs for a bit and eventually times out.

The API is being called by other applications in house, so we know its not a server-side, or networking issue. We've tried on several machines - all of which have the same problem. Has anyone ever had this problem before, or does anyone have any sugestions about how to debug (since the response object doesn't yeild anything, or at least nothing useful).

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

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

发布评论

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

评论(2

念﹏祤嫣 2024-07-18 19:46:17

如果您不处理 WebResponse,通常会发生这种情况。 从客户端到同一台计算机的连接数有限制,默认情况下为两个。 如果关闭 WebResponse,则可以重用(或关闭)连接。 using 语句是你的朋友:

WebRequest request = [...];
// Do stuff with the request stream here (and dispose it)
using (WebResponse response = request.GetResponse())
{
    // Stuff with the response
}

This usually happens if you're not disposing the WebResponse. There's a limit applied to the number of connections from a client to the same machine, and by default it's two. The connections can be reused (or closed) if you close the WebResponse. The using statement is your friend here:

WebRequest request = [...];
// Do stuff with the request stream here (and dispose it)
using (WebResponse response = request.GetResponse())
{
    // Stuff with the response
}
爱她像谁 2024-07-18 19:46:17

是的,你完全正确。 响应没有得到正确处理。 我们一直把这个留给垃圾收集器,你猜对了,垃圾收集器没有及时收集。 不幸的是,我关闭了浏览器并忘记阅读任何答案(哈哈,你认为我感觉有多愚蠢),问题就解决了。

今晚我学到了两件事。 1、妥善处理您的WebRequest; 2、更加注意堆栈溢出的答案!

Yep, your exactly right. The response wasn't being disposed properly. We'd been leaving this upto the garbage collector, which, you guessed it, wasnt being collected in time. Unfortunately, I closed my browser and forgot to read any answers (lol how stupid do you think I feel) and the problem is solved.

I've learned 2 things tonight. 1, dispose of your WebRequests properly; and 2, PAY MORE ATTENTION TO STACK-OVERFLOW ANSWERS!

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