REST接口GET方法获取文件保存到本地(C#)
REST接口GET方法获取文件保存到本地(C#)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace testConsoleApplication1 { class Class1 { static void Main(string[] args) { string urlstr = @"http://127.0.0.1/open/files/***files"; //string urlstr = @"http://127.0.0.1/open/files/***files/1.pdf"; //"http://creditcard.bankcomm.com/jhbc/download/discount/baiwan_c3.pdf"; //"http://127.0.0.1/open/files/***files/1.pdf"; //FileStream fs = new FileStream(@"d:\***1.pdf", FileMode.Create, FileAccess.Write); FileStream fs = new FileStream(@"d:\filesList.xml", FileMode.Create, FileAccess.Write); Uri url = new Uri(urlstr); HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); myHttpWebRequest.Accept = "application/xml"; myHttpWebRequest.Headers.Add("X-***-Token-Id","***"); myHttpWebRequest.Method = "GET"; HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Console.WriteLine(myHttpWebResponse.StatusCode.ToString()); Stream receiveStream = myHttpWebResponse.GetResponseStream(); Byte[] bytes = new Byte[100]; int count = receiveStream.Read(bytes, 0, 100); while (count != 0) { fs.Write(bytes, 0, count); count = receiveStream.Read(bytes, 0, 100); } fs.Close(); receiveStream.Close(); myHttpWebResponse.Close(); } } }