public static string GetWebSiteContent(string url, string type) { try { //CrawlingData cData = new CrawlingData(); string _requestUrl = url.Trim(); string _encodingType = "utf-8"; FileWebRequest request = (FileWebRequest)WebRequest.Create(_requestUrl); //request..AllowAutoRedirect = true; //request.MaximumAutomaticRedirections = 3; //request.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Arya.NET Robot)"; //request. = true; request.Timeout = 15 * 1000; System.Net.FileWebResponse response = (FileWebResponse)request.GetResponse(); //showExtractConetent.InnerHtml = ((HttpWebResponse)response).StatusDescription; //showExtractConetent.InnerHtml += "<br/>"; //_encodingType = GetEncodingType(response); System.IO.StreamReader reader = new System.IO.StreamReader (response.GetResponseStream(), Encoding.GetEncoding("utf-8")); // Read the content. string responseFromServer = reader.ReadToEnd(); return(responseFromServer); } catch (WebException ex) { string response = GetWebSiteContent(url); return(response); } }
/// <summary> /// 下载局域网文件 /// </summary> /// <param name="path">局域网文件路径,如:\\192.168.10.1\app\app\123.zip</param> /// <param name="username">计算机名称</param> /// <param name="password">计算机密码</param> static void RequestWindowsShared(string path, string username, string password) { System.Net.FileWebRequest request = null; System.Net.FileWebResponse response = null; System.IO.Stream stream = null; System.IO.FileStream fileStream = null; try { Uri uri = new Uri(path); request = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uri); System.Net.ICredentials ic = new System.Net.NetworkCredential(username, password); request.Credentials = ic; response = (System.Net.FileWebResponse)request.GetResponse(); stream = response.GetResponseStream(); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); string filename = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + System.IO.Path.GetFileName(path); fileStream = new FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); fileStream.Write(bytes, 0, bytes.Length); fileStream.Flush(); Console.WriteLine("下载成功!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { fileStream.Close(); fileStream.Dispose(); stream.Close(); stream.Dispose(); } }