C# WinForm 通过URL取得服务器上的某图片文件到本地

方法1示例代码:
--------------
string strImageURL = "http://192.168.0.1:88/VDirA/images/1.jpg";

System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile(strImageURL, @"D:\1.jpg");

方法2示例代码:
--------------
string strImageURL = "http://192.168.0.1:88/VDirA/images/1.jpg";

System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strImageURL);
webRequest.Method = "GET";
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();

System.IO.Stream s = webResponse.GetResponseStream();

List<byte> list = new List<byte>();
while (true)
{
    int data = s.ReadByte();
    if (data == -1)
        break;
    else
    {
        byte b = (byte)data;
        list.Add(b);
    }
}
byte[] bb = list.ToArray();
System.IO.File.WriteAllBytes("C://1.jpg", bb);
s.Close();

posted on 2008-12-03 08:36  freeliver54  阅读(5983)  评论(0编辑  收藏  举报

导航