gpt4 book ai didi

HttpWebRequest ReadWriteTimeout 在 .NET 中被忽略;在单声道工作

转载 作者:行者123 更新时间:2023-12-01 13:06:36 26 4
gpt4 key购买 nike

将数据写入 Web 服务器时,我的测试显示 HttpWebRequest.ReadWriteTimeout 被忽略,与 MSDN spec 相反.例如,如果我将 ReadWriteTimeout 设置为 1(=1 毫秒),调用 myRequestStream.Write() 传递一个需要 10 秒传输的缓冲区,它传输成功并且永远不会使用 .NET 3.5 SP1 超时。在 Mono 2.6 上运行的相同测试如预期的那样立即超时。有什么问题吗?

最佳答案

似乎存在一个错误,当在 BeginGetRequestStream() 返回给您的 Stream 实例上设置写入超时时,它不会向下传播到 native 套接字。我将提交错误以确保此问题在 .NET Framework 的 future 版本中得到纠正。

这是一个解决方法。

private static void SetRequestStreamWriteTimeout(Stream requestStream, int timeout)
{
// Work around a framework bug where the request stream write timeout doesn't make it
// to the socket. The "m_Chunked" field indicates we are performing chunked reads. Since
// this stream is being used for writes, the value of this field is irrelevant except
// that setting it to true causes the Eof property on the ConnectStream object to evaluate
// to false. The code responsible for setting the socket option short-circuits when it
// sees Eof is true, and does not set the flag. If Eof is false, the write timeout
// propagates to the native socket correctly.

if (!s_requestStreamWriteTimeoutWorkaroundFailed)
{
try
{
Type connectStreamType = requestStream.GetType();
FieldInfo fieldInfo = connectStreamType.GetField("m_Chunked", BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(requestStream, true);
}
catch (Exception)
{
s_requestStreamWriteTimeoutWorkaroundFailed = true;
}
}

requestStream.WriteTimeout = timeout;
}

private static bool s_requestStreamWriteTimeoutWorkaroundFailed;

关于HttpWebRequest ReadWriteTimeout 在 .NET 中被忽略;在单声道工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2679192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com