-2

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

4

1 回答 1

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

于 2012-10-29T22:11:24.853 回答