阅读 GetResponseStream() 的最佳方式是什么?
- 作者: 咚咚85055973
- 来源: 51数据库
- 2022-12-15
问题描述
从 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();
}
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
