/// <summary> /// 获取远程文件源代码 WebClient UploadData /// </summary> /// <param name="url">远程url</param> /// <param name="encoding">编码</param> /// <returns>获取远程文件源代码</returns> public static string GetRemoteHtmlCode5(string url, System.Text.Encoding encoding = null) { url += (url.IndexOf("?") >= 0 ? "&time=" : "?time=") + Rand.RndDateStr(); string postString = ""; using (WebClient webClient = new WebClient()) { webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); byte[] postData = Encoding.ASCII.GetBytes(postString); byte[] responseData = webClient.UploadData(url, "POST", postData); string srcString = encoding.IfNull(Encoding.UTF8).GetString(responseData); return srcString; } }
/// <summary> /// 获取远程文件源代码 HttpWebRequest UserAgent + Referer + AllowAutoRedirect /// </summary> /// <param name="url">url</param> /// <param name="encoding">编码</param> /// <param name="timeout">超时时间</param> /// <returns>获取远程文件源代码</returns> public static string GetRemoteHtmlCode3(string url, System.Text.Encoding encoding = null, int timeout = 0) { url += (url.IndexOf("?") >= 0 ? "&time=" : "?time=") + Rand.RndDateStr(); string s = ""; HttpWebResponse response = null; StreamReader stream = null; try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.AllowAutoRedirect = true; if (timeout > 1) request.Timeout = timeout; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Referer = url; response = (HttpWebResponse)request.GetResponse(); stream = new StreamReader(response.GetResponseStream(), encoding.IfNull(Encoding.UTF8)); s = stream.ReadToEnd(); } catch { } finally { if (stream.IsNotNull()) stream.Close(); if (response.IsNotNull()) response.Close(); } return s; }
/// <summary> /// 获取远程文件源代码 WebClient DownloadData /// </summary> /// <param name="url">url</param> /// <param name="encoding">编码</param> /// <returns>获取远程文件源代码</returns> public static string GetRemoteHtmlCode4(string url, System.Text.Encoding encoding = null) { url += (url.IndexOf("?") >= 0 ? "&time=" : "?time=") + Rand.RndDateStr(); using (WebClient wc = new WebClient()) { #if !MONO40 wc.Credentials = CredentialCache.DefaultCredentials; #endif Byte[] pageData = wc.DownloadData(url); string content = encoding.IfNull(Encoding.UTF8).GetString(pageData); return content; } }
/// <summary> /// 获取远程文件源代码 good HttpWebRequest /// </summary> /// <param name="url">url</param> /// <param name="encoding">编码</param> /// <param name="timeout">超时时间</param> /// <returns>获取远程文件源代码 线程安全</returns> public static string GetRemoteHtmlCode2(string url, System.Text.Encoding encoding = null, int timeout = 0) { url += (url.IndexOf("?") >= 0 ? "&time=" : "?time=") + Rand.RndDateStr(); string s = ""; HttpWebResponse response = null; StreamReader stream = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); if (timeout > 1) request.Timeout = timeout; response = (HttpWebResponse)request.GetResponse(); stream = new StreamReader(response.GetResponseStream(), encoding.IfNull(Encoding.UTF8)); s = stream.ReadToEnd(); } catch { } finally { if (stream.IsNotNull()) stream.Close(); if (response.IsNotNull()) response.Close(); } return s; }