1. 程式人生 > >C# WinForm 通過URL取得伺服器上的某圖片檔案到本地

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();