如何从网络空间用c#读取文件? 如何从“www.mysite.com/ver.txt”读取文件,如果文件中的文字是“1” 返回值“OK”但如果文件中的文本为“0”则返回值“否”。 谢谢。
答案 0 :(得分:2)
您可以使用HttpWebRequest
课程。这样的事情应该有效:
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
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?
String text = sb.ToString();
if(text == "1")
Console.Write("Yes");
else
Console.Write("No");