HttpWebRequests 在后续调用中失败 [英] HttpWebRequests Failing on Subsequent Calls

查看:17
本文介绍了HttpWebRequests 在后续调用中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个模糊的问题,特别是因为我没有提供任何代码,但我正在开发一个 .Net 2.0 应用程序,并且我们有一个 WebRequest,它可以将数据发布到内部构建的 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.

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

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.

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

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).

推荐答案

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

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
}

这篇关于HttpWebRequests 在后续调用中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆