C# 利用代理爬去网页

// yanggang@mimvp.com
// http://proxy.mimvp.com
// 2015-11-09

using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication1
{
	class Program
	{
		public static void Main(string[] args)
		{
			System.Net.WebProxy proxy = new WebProxy("218.21.230.156", 443);		// "107.150.96.188", 8080
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://proxy.mimvp.com");
			request.Proxy = proxy;
			using (WebResponse response = request.GetResponse())
			{
				using (TextReader reader = new StreamReader(response.GetResponseStream()))
				{
					string line;
					while ((line = reader.ReadLine()) != null)
						Console.WriteLine(line);
				}
			}
		}

		public static void Main2()
		{
			// your code goes here
			System.Net.WebProxy proxy = new WebProxy("107.150.96.188", 8080);
			System.Net.HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
			req.Proxy = proxy;
			req.Timeout = 30 * 1000;
			System.Net.HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
			Encoding bin = Encoding.GetEncoding("UTF-8");
			StreamReader reader = new StreamReader(resp.GetResponseStream(), bin);
			string str = reader.ReadToEnd();
			Console.WriteLine(str);
		
			reader.Close();
			reader.Dispose();
		}
	}
}

获取更多代理,请访问米扑代理:

http://proxy.mimvp.com

 

 

参考推荐:

C#中HttpWebRequest使用Proxy

C# HttpWebRequest通过代理服务器抓取网页内容应用介绍

C#如何实现IE代理设置