如何从互联网读取文件? [英] How to read a file from internet?
本文介绍了如何从互联网读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个简单的问题: 我有一个在线文件(txt).如何阅读并检查其内容? (C#.net 2.0)
simple question: I have an file online (txt). How to read it and check if its there? (C#.net 2.0)
推荐答案
来自 http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("myurl");
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
Console.WriteLine(sb.ToString());
这篇关于如何从互联网读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文