从 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() 的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!