如何从网络空间用c#读取文件

时间:2012-10-29 22:03:17

标签: c# html web

如何从网络空间用c#读取文件? 如何从“www.mysite.com/ver.txt”读取文件,如果文件中的文字是“1” 返回值“OK”但如果文件中的文本为“0”则返回值“否”。 谢谢。

1 个答案:

答案 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");

http://www.csharp-station.com/HowTo/HttpWebFetch.aspx