HttpWebOperation 超时。为什么以及要检查什么?

发布于 2025-01-03 06:17:22 字数 1015 浏览 0 评论 0原文

我像这样调用 Web 服务(我的 Web 服务):

var request = WebRequest.Create(Options.ServerUri + Options.AccountId + "/integration/trip") as HttpWebRequest;
request.Timeout = 20000; // 20 seconds should be plenty, no need for 100 seconds
request.ContentType = "application/json";
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(Options.LoginName + ":" + Options.Password)));
request.Method = "POST";

var serializedData = (new JavaScriptSerializer()).Serialize(trip);

var bytes = Encoding.UTF8.GetBytes(serializedData);

request.ContentLength = bytes.Length;
var os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();

request.GetResponse();

LoggingAndNotifications.LogAndNotify(string.Format("Success uploading trip: {0}", trip.TripId), false);

return true;

此代码重复调用以发布新对象。大约 3 次调用后,我开始在 reguest.GetReponse() 上超时

。服务器端没有错误,事件日志中没有任何错误。感觉就像“有什么东西”阻止我反复点击服务。我应该寻找什么?公司防火墙可以吗?还是我的代码有问题?

I'm calling web service (my web service) like this:

var request = WebRequest.Create(Options.ServerUri + Options.AccountId + "/integration/trip") as HttpWebRequest;
request.Timeout = 20000; // 20 seconds should be plenty, no need for 100 seconds
request.ContentType = "application/json";
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(Options.LoginName + ":" + Options.Password)));
request.Method = "POST";

var serializedData = (new JavaScriptSerializer()).Serialize(trip);

var bytes = Encoding.UTF8.GetBytes(serializedData);

request.ContentLength = bytes.Length;
var os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();

request.GetResponse();

LoggingAndNotifications.LogAndNotify(string.Format("Success uploading trip: {0}", trip.TripId), false);

return true;

This code called repeatedly to post new objects. After about 3 calls I start getting timeouts on reguest.GetReponse()

There is no errors on server side, nothing in Event Log. It feels like "something" stops me from repeatedly hitting service. What should I look for? Is it possible with company firewall? Or something wrong with my code?

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

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

发布评论

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

评论(3

谁对谁错谁最难过 2025-01-10 06:17:22

我认为问题在于您没有关闭回复。尝试按如下方式编辑您的代码:

var response = request.GetResponse() as HttpWebResponse;
response.Close();

I think the issue is that you are not closing the response. Try editing your code as follows:

var response = request.GetResponse() as HttpWebResponse;
response.Close();
童话 2025-01-10 06:17:22

您应该按照

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

// Return the response. 
WebResponse myResponse = myRequest.GetResponse();

// Code to use the WebResponse goes here.

// Close the response to free resources.
myResponse.Close();

唔。 doco还说

此类型的任何公共静态(在 Visual Basic 中共享)成员都是
线程安全。不保证任何实例成员都是线程
安全。

您可能应该要求某种锁。

You should close the response as per the example in the doco.

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

// Return the response. 
WebResponse myResponse = myRequest.GetResponse();

// Code to use the WebResponse goes here.

// Close the response to free resources.
myResponse.Close();

Hmm. The doco also says

Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe.

You should probably ask for a lock of some kind.

同展鸳鸯锦 2025-01-10 06:17:22

您确定这不是服务器端错误引起的吗?

这似乎很奇怪,据我所知,.net4上的Web请求是基于底层的IOCP,也许你可以尝试在每次循环后释放Web请求/响应资源。

由于 GetResponse() 将返回一个流,如果您不从中读取,则真实数据可能不会从服务器传输到客户端。 (我在尝试解析使用 peek() 的响应时发现了这一点,并且它总是返回无效值,直到调用 read() 为止。)

因此,尝试读取它或直接关闭它。

Are you sure this is not caused by server side bugs?

It seems strange, as far as I known, the webrequest on .net4 is based on IOCP in lower layer, maybe you can try release web request/response resources after each loop.

Since the GetResponse() will return a stream, if you don't read from it, the real data may not transfer from server to client side. (I found this when I am trying to parse a response that I used peek(), and it always return an invalid value until the read() is called.)

So, try to read it or just close it.

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