http下载文件——HttpWebRequest

作者:追风剑情 发布于:2015-10-28 15:14 分类:C#

using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Text;

namespace WebRequestTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                HttpWebRequest web_req = (HttpWebRequest)WebRequest.Create("http://192.168.1.211/android_res/version.txt");
                HttpWebResponse web_resp = (HttpWebResponse)web_req.GetResponse();
                long contentLength = web_resp.ContentLength;
                byte[] read_bytes = new byte[2048];
                int read_size = web_resp.GetResponseStream().Read(read_bytes, 0, read_bytes.Length);
                string content = Encoding.UTF8.GetString(read_bytes);
                web_resp.Close();

                Console.WriteLine("文件长度: " + contentLength);
                Console.WriteLine("文件内容:");
                Console.WriteLine(content);

                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

运行效果

hhhhh.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号