如何从这个URL从文件内容? [英] How to get content from file from this URL?

查看:94
本文介绍了如何从这个URL从文件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网址:从谷歌


$ b $网址b

在打开链接在新标签页,浏览器逼我下载它。下载后,我得到一个名为S的文本文件。但我想用C#访问这个URL,并得到它的文字,不要将其保存为文件计算机。什么办法做到这一点?


解决方案

  VAR的WebRequest = WebRequest.Create(@ HTTP:// yourURL的); 

使用(VAR响应= webRequest.GetResponse())
使用(VAR含量= response.GetResponseStream())使用
(VAR读者=新的StreamReader(内容)){
VAR strContent = reader.ReadToEnd();
}

这将会把请求的内容到strContent。



或者像 adrianbanks下面简单地使用的:// msdn.microsoft.com/en-us/library/fhd1f0sw.aspx> WebClient.DownloadString()


I have this URL: URL from Google

When open link in new tab, the browser force me download it. After download, I get a text file named "s". But I want use C# access to this URL and get it's text, don't save it as a file to computer. Is any way to do this?

解决方案

var webRequest = WebRequest.Create(@"http://yourUrl");

using (var response = webRequest.GetResponse())
using(var content = response.GetResponseStream())
using(var reader = new StreamReader(content)){
    var strContent = reader.ReadToEnd();
}

This will place the contents of the request into strContent.

Or as adrianbanks mentioned below simply use WebClient.DownloadString()

这篇关于如何从这个URL从文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆