How to read a file from internet?

后端 未结 7 1882
甜味超标
甜味超标 2020-12-03 03:05

simple question: I have an file online (txt). How to read it and check if its there? (C#.net 2.0)

相关标签:
7条回答
  • 2020-12-03 03:40

    from 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());
    
    0 讨论(0)
提交回复
热议问题