using System; using System.IO; using System.Net; namespace test { class Program { static void Main(string[] args) { try { string url = "http://192.168.1.1/test.txt"; HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url); Request.Timeout = 5; HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); Stream dataStream = Response.GetResponseStream(); byte[] buffer = new byte[8192]; FileStream fs = new FileStream("C:\\test.txt",FileMode.Create,FileAccess.Write); int size = 0; do { size = dataStream.Read(buffer, 0, buffer.Length); if (size > 0) fs.Write(buffer, 0, size); } while (size > 0); fs.Close(); Response.Close(); Console.WriteLine("Get File Done!!\n"); Console.ReadKey(); } catch(WebException ex) { Console.WriteLine("Get Nothing!!\n"); Console.ReadKey(); } } } }
2012年10月30日 星期二
使用HttpWebRequest抓取檔案
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言