Windows 8 C# – 将网页源检索为字符串
发布时间:2020-07-16 10:55:27 所属栏目:Windows 来源:互联网
导读:有一个教程实际上适用于带有XAML和C#: http://www.tech-recipes.com/rx/1954/get_web_page_contents_in_code_with_csharp/的Windows 8平台 这是如何做: HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);myRequest.Met
有一个教程实际上适用于带有XAML和C#: http://www.tech-recipes.com/rx/1954/get_web_page_contents_in_code_with_csharp/的Windows 8平台 这是如何做: HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL); myRequest.Method = "GET"; WebResponse myResponse = myRequest.GetResponse(); StreamReader sr = new StreamReader(myResponse.GetResponseStream(),System.Text.Encoding.UTF8); string result = sr.ReadToEnd(); sr.Close(); myResponse.Close(); 但是在Windows 8中,最后两行是关闭连接的代码(我假设),检测到错误.尽管如此,它在没有关闭连接的情况下工作正常,但有什么可能性?为什么我们必须关闭连接?如果我不这样做可能会出错? “关闭连接”甚至意味着什么? 如果您正在为Windows 8开发,则应考虑使用异步方法来提供更好的用户体验,这是推荐的新标准.您的代码将如下所示:public async Task<string> MakeWebRequest(string url) { HttpClient http = new System.Net.Http.HttpClient(); HttpResponseMessage response = await http.GetAsync(url); return await response.Content.ReadAsStringAsync(); } (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- 标签为Windows的SSH工具?
- Win10远程桌面 出现 身份验证错误,要求的函数不
- Windows – 由于MSVCR110.dll,MS Visual Studio
- Windows Azure开发存储blob服务不启动
- Win2008 r2 IIS7.5制定目录禁止执行脚本的方法
- windows7下composer安装不了或composer命令无效的
- 如何配置在Windows上运行的node.js以使用具有多个
- windows – 如何在git bash中注册新安装的驱动器
- winapi – Windows XP与Vista的SetThreadUILangu
- windows-vista – 使用托管代码调用死亡蓝屏
热点阅读