使用HttpWebRequest WebClien获取资源
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { WebProxy[] proxys = new WebProxy[20];//设置代理 proxys[0] = new WebProxy("69.197.148.18:7808"); proxys[1] = new WebProxy("199.200.120.37:7808"); proxys[2] = new WebProxy("107.182.17.9:7808"); proxys[3] = new WebProxy("104.238.200.211:7808"); proxys[4] = new WebProxy("162.208.49.45:3127"); proxys[5] = new WebProxy("128.199.226.223:3128"); proxys[6] = new WebProxy("167.114.24.220:7808"); string url = @"http://www.proxz.com/proxy_list_transparent_2.html"; Random r = new Random(); int num = r.Next(0, proxys.Length); string html = GetHtml(url, proxys[num]); int statr = html.IndexOf("eval(unescape('"); int end = html.IndexOf("'));</script>"); string lastHtml = string.Empty;//%73%65%6c%66%2e%64%6f%63%75%6d%65%6e%7 if (statr != -1 && end != -1 && statr < end) { lastHtml = html.Substring(statr + 15, end - statr - 15); } string htmlIp = System.Web.HttpUtility.UrlDecode(lastHtml);//对上述字符串进行解码 } private static string GetHtml(string url, WebProxy proxy) { HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;//创建一个对象 if (httpRequest == null) return null; httpRequest.Method = "GET";//设置请求方式:GET/POST httpRequest.Timeout = 2000;//设置等待时间(以毫秒为单位) if (proxy != null)//判断代理是否为空(因为如果使用多线程有些网站会限制同一个IP多线程访问的,故需要使用代理斯骗代理网站:http://www.proxz.com) { httpRequest.Proxy = proxy; } Stream responseStream; try { System.Net.ServicePointManager.Expect100Continue = false; #region 【http之100-continue】详解 // 【http之100-continue】 //1、http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST数据,如果处理,则POST上传数据。在现实应用中,通过在POST大数据时,才会使用100-continue协议。 //2、客户端策略。 // 1)如果客户端有POST数据要上传,可以考虑使用100-continue协议。加入头{"Expect":"100-continue"} // 2)如果没有POST数据,不能使用100-continue协议,因为这会让服务端造成误解。 // 3)并不是所有的Server都会正确实现100-continue协议,如果Client发送Expect:100-continue消息后,在timeout时间内无响应,Client需要立马上传POST数据。 // 4)有些Server会错误实现100-continue协议,在不需要此协议时返回100,此时客户端应该忽略。 //3、服务端策略。 // 1)正确情况下,收到请求后,返回100或错误码。 // 2)如果在发送100-continue前收到了POST数据(客户端提前发送POST数据),则不发送100响应码(略去)。 #endregion HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();//调用httpRequest对象方向请求服务器url并返回访问资源 responseStream = wr.GetResponseStream();//将读到资源存放在responseStream容器中 if (responseStream != null) { string stringResponse = string.Empty; using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8))//创建读取对象并设定处量的资源responseStream与处理方式Encoding.UTF8 { stringResponse = responseReader.ReadToEnd(); } responseStream.Close();// 关闭当前流并释放与之关联的所有资源(如套接字和文件句柄)。 不直接调用此方法,而应确保流得以正确释放。(则释放对应的资源让其他程序使用,否则一直占用) return stringResponse;//反回文本 } } catch (Exception ex) { return ex.Message; } return null; } } } HttpWebRequest获取并解码
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string html= GetTextFromUrl("http://www.cnblogs.com/wiseblog/p/4318833.html", Encoding.UTF8); } public static string GetTextFromUrl(string url, Encoding encoding) { if (!string.IsNullOrEmpty(url)) { if (encoding == null) { encoding = Encoding.UTF8; } try { using (WebClient wc = new WebClient()) { GlobalProxySelection.Select = new WebProxy("49.50.96.71:443");//设置代理 byte[] arr = wc.DownloadData(url);//调用DownloadData获取资源得玻BYTE数组 string result = encoding.GetString(arr);//调用encoding.GetString方法取把数组以指定解码方式UTF8解码成字符串 return result;//返回结果 } } catch (System.Exception err) { return err.ToString(); } } return null; } } } WebClient获取资源
HttpWebRequest using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace 下载图片 { class Program { static void Main(string[] args) { string url = "http://news.youdao.com/?keyfrom=fanyi.top"; string imgUrl = "http://picture.youth.cn/qtdb/201503/t20150306_6507030.htm"; string savePath = @"D:\windowSer\horseService\HTMLTEXT\dd.jpg"; DownImg(url, imgUrl, savePath); Console.ReadKey(); } /// <summary> /// /// </summary> /// <param name="url">请求网页地址</param> /// <param name="imgUrl">下载网页图片地址</param> /// <param name="savePath">保存图片地址(含图片名称)</param> public static void DownImg(string url, string imgUrl, string savePath) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl); request.Referer = url;//设置访问的网页地址(因为有些网页要有相关限制,因些需要告知服务器) WebResponse response = request.GetResponse();//取得请求资源 Stream reader = response.GetResponseStream(); FileStream writer = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write); byte[] buff = new byte[512]; int c = 0; //实际读取的字节数 while ((c = reader.Read(buff, 0, buff.Length)) > 0) { writer.Write(buff, 0, c); } writer.Close(); writer.Dispose(); reader.Close(); reader.Dispose(); response.Close(); } } } 下载图片
浙公网安备 33010602011771号