gpt4 book ai didi

c# - HttpWebResponse.ReadTimeout - 不支持超时?

转载 作者:行者123 更新时间:2023-11-30 16:25:32 30 4
gpt4 key购买 nike

我们遇到一个问题,在我们产品的单个实例上,当我们尝试设置 System.Net.HttpWebResponse 对象的 ReadTimeout 属性时收到 InvalidOperationException 异常。

这个问题只发生在一个实例上,我们有许多没有这个问题的实时网站。我们已尝试在本地重现该问题,但无济于事。

下面的代码说明了这个问题。

非常欢迎任何想法。

谢谢

    private static XmlReader GenerateReaderFromResponse(HttpWebResponse response, HttpWebRequest request)
{
Stream responseStream = response.GetResponseStream();
responseStream.ReadTimeout = request.Timeout; //This is where the exception is generated - System.InvalidOperationException: Timeouts are not supported on this stream.

using (StreamReader responseReader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ProhibitDtd = false;
string responseContent = responseReader.ReadToEnd();
return XmlReader.Create(new StringReader(responseContent), readerSettings);
}
}

最佳答案

您需要的是 HttpWebRequest.ReadWriteTimeout属性(property)。

它指定在响应 Stream 上的读(或写)操作超时之前的毫秒数,抛出一个 WebException with Status设置为 WebExceptionStatus .请求取消

来自msdn documentation :

The ReadWriteTimeout property is used when writing to the stream returned by the GetRequestStream method or reading from the stream returned by the GetResponseStream method.

Specifically, the ReadWriteTimeout property controls the time-out for the Read method, which is used to read the stream returned by the GetResponseStream method, and for the Write method, which is used to write to the stream returned by the GetRequestStream method.

To specify the amount of time to wait for the request to complete, use the Timeout property.

关于c# - HttpWebResponse.ReadTimeout - 不支持超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791423/

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