What is the best way to read GetResponseStream()?(阅读 GetResponseStream() 的最佳方式是什么?)
问题描述
从 GetResponseStream 读取 HTTP 响应的最佳方式是什么?
What is the best way to read an HTTP response from GetResponseStream ?
目前我正在使用以下方法.
Currently I'm using the following approach.
Using SReader As StreamReader = New StreamReader(HttpRes.GetResponseStream)
SourceCode = SReader.ReadToEnd()
End Using
我不太确定这是否是读取 http 响应的最有效方式.
I'm not quite sure if this is the most efficient way to read an http response.
我需要将输出作为字符串,我见过一个 文章采用不同的方法,但我不太确定它是否是一个好方法.在我的测试中,代码在不同的网站上存在一些编码问题.
I need the output as string, I've seen an article with a different approach but I'm not quite if it's a good one. And in my tests that code had some encoding issues with in different websites.
您如何阅读网络回复?
推荐答案
我对字符串进行处理的简单方法.请注意 StreamReader
构造函数上的 true
第二个参数.这告诉它从字节顺序标记检测编码,并且可能有助于解决您遇到的编码问题.
My simple way of doing it to a string. Note the true
second parameter on the StreamReader
constructor. This tells it to detect the encoding from the byte order marks and may help with the encoding issue you are getting as well.
string target = string.Empty;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=583");
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
try
{
StreamReader streamReader = new StreamReader(response.GetResponseStream(),true);
try
{
target = streamReader.ReadToEnd();
}
finally
{
streamReader.Close();
}
}
finally
{
response.Close();
}
这篇关于阅读 GetResponseStream() 的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:阅读 GetResponseStream() 的最佳方式是什么?


- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 使用 rss + c# 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01