使用代理(WebProxy)爬虫
2015-04-23 10:30
176 查看
关键代码:
View Code
参考资料:
http://www.haolizi.net/example/view_199.html
/// <summary> /// 发送get请求 /// </summary> /// <param name="strUrl"></param> /// <param name="isRetry"></param> /// <returns></returns> private string GetHtmlByUrl(string strUrl, bool isRetry = false, WebProxy daili = null) { currentDailiExecCount++; if (currentDailiExecCount > dailiExecMaxCount) { logclass.Debug("======当前代理======" + currentdaili.Address.Authority + ":" + currentdaili.Address.Port + "==跑的次数超过了设置的最大次数(" + dailiExecMaxCount.ToString()+")"); } try { HttpWebResponse response = new webframework.common.HttpHelper() { URL = string.Format("{0}", strUrl), //Proxy = daili == null ? currentdaili : daili, //Proxy = new WebProxy("218.204.140.97:8118", true), Proxy = daili == null ? (currentDailiExecCount > dailiExecMaxCount ? GetOneDaili() : currentdaili) : daili, Timeout = 5 * 1000, }.CreateGetHttpResponse(); return response.HttpString(Encoding.UTF8); } catch (Exception) { //重试请求 if (!isRetry) return GetHtmlByUrl(strUrl, true, GetOneDaili()); else throw null; } } /// <summary> /// 发送post请求 /// </summary> /// <param name="strUrl"></param> /// <param name="isRetry"></param> /// <returns></returns> private string PostHtmlByUrl(string strUrl, string strPostString, bool isRetry = false, WebProxy daili = null) { currentDailiExecCount++; if (currentDailiExecCount > dailiExecMaxCount) { logclass.Debug("======当前代理======" + currentdaili.Address.Authority + ":" + currentdaili.Address.Port + "==跑的次数超过了设置的最大次数(" + dailiExecMaxCount.ToString()+")"); } try { HttpWebResponse response = new HttpHelper() { URL = strUrl, PostString = strPostString, //Proxy = new WebProxy("218.204.140.97:8118", true), Proxy = daili == null ? (currentDailiExecCount>dailiExecMaxCount?GetOneDaili(): currentdaili) : daili, //Proxy = daili == null ? currentdaili : daili, PostEncoding = Encoding.UTF8, Timeout = 5 * 1000, }.CreatePostHttpResponse(); return response.HttpString(Encoding.UTF8); } catch (Exception) { //重试请求 if (!isRetry) return PostHtmlByUrl(strUrl, strPostString, true, GetOneDaili()); else throw null; } }
View Code
参考资料:
http://www.haolizi.net/example/view_199.html
相关文章推荐
- CentOS 6下,远程安装KVM虚拟机,并使用apache mod_proxy反向代理建立多个web服务器
- nginx反向代理tomcat集群达到负载均衡,同时使用proxy_cache做web缓存 推荐
- Python爬虫使用代理proxy抓取网页
- Python开发中爬虫使用代理proxy抓取网页的方法示例
- nginx使用proxy_pass反向代理时,session丢失的问题
- python使用代理爬虫例子
- iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
- 使用nodejs中httpProxy代理时候出现404异常的解决方法
- 【python爬虫】python使用代理爬虫例子
- 【动态代理】使用构造方法创建代理类、使用 newProxyInstance.newProxyInstance()创建代理类
- 使用代理软件mysql-proxy实现mysql的读写分离
- (8) 使用JDK中的Proxy技术实现AOP功能 以及 CGLIB生成代理
- extjs中的数据代理proxy的简单使用
- 使用动态代理实现一个简单web客户端
- 在基于vue的webpack脚手架开发中使用了代理转发,结果浏览器发出的请求中不带cookie导致登录时总是session失效怎么办?
- 3proxy+nginx实现sock5爬虫代理
- 使用webpack配置vue项目代理 (超简单)
- 【JavaWeb-14】代理Proxy、动态代理、AOP编程思想、注解、元注解、类加载器
- 使用nginx反向代理后如何在后台web应用中获取用户ip
- 使用TransactionProxyFactoryBean代理事务