作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚注意到 C# 中的某些行为让我有些不快。我正在使用 C# 5 和 .NET 4.5。当我在 HTTPResponse 对象上调用 GetResponseStream() 时,我能够获得响应流,但如果我在同一个对象上再次调用它,则响应为空。
// Works! Body of the response is in the source variable.
HttpResponse response = (HttpWebResponse)request.GetResponse();
String source = new StreamReader(response.GetResponseStream()).ReadToEnd();
// Does Not Work. Source is empty;
String source2 = new StreamReader(response.GetResponseStream()).ReadToEnd();
以上只是一个例子来说明问题。
编辑
这就是我想要做的。基本上,如果事件附加到 HTTP 对象,它会将响应传递回回调方法。
HttpWebResponse public Get(String url)
{
// HttpWebRequest ...
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// postRequest is an event handler. The response is passed to the
// callback to do whatever it needs to do.
if (this.postRequest != null)
{
RequestEventArgs requestArgs = new RequestEventArgs();
requestArgs.source = response;
postRequest.Invoke(this, requestArgs);
}
return response;
}
在回调方法中,我可能想检查响应的主体。如果这样做,我将在 Get() 返回响应时丢失响应中的数据。
最佳答案
响应流直接从网络连接读取。
一旦读到最后(第 2 行),就没有更多数据可读了。
关于c# - 为什么我只能调用一次 GetResponseStream(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17891736/
有一条(相对)众所周知的 Perl 公理:“只有 Perl 可以解析 Perl”。我想知道 Perl 6 是否仍然如此? 扩大讨论...考虑到 PyPy 最近的更新,我想到了这个问题。 Perl 独特
这是设置。在上一个问题中,我发现我可以通过子组件中的状态传递对象属性,然后使用 componentDidUpdate 获取该对象属性。在这种情况下,状态和属性都称为到达。 这是基本代码... expo
我运行的是 10.5.2 社区版。我已经标记了 源/主要/资源 作为源目录。我可以右键单击并“编译”某些文件,据我所知,这意味着 IDE 将文件复制到与发送类文件的“com.mydomain.pack
我是一名优秀的程序员,十分优秀!