http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 61001|回复: 7

[求助] httphelper类在引用时,用代理的情况,如果代理不可用,经常出错。

[复制链接]
发表于 2014-4-18 07:48:17 | 显示全部楼层 |阅读模式
httphelper类在引用时,用代理的情况,如果代理不可用,经常出错。
[C#] 纯文本查看 复制代码
 private void GetCode()//获取验证码
        {
            if (cbProxy.Checked == true)
            {
                if (proxyipnum < proxylist.Count)
                {
                    proxyip = proxylist[proxyipnum];
                    proxyipnum++;
                    lblProxy.Text = "当前使用的代理是:" + proxyip;
                }
            }
            item = new HttpItem()
            {
                URL = "http://www.sqee.cn/member.php?mod=register",
                Method = "GET",
                ProxyIp = proxyip,
            };
            result = http.GetHtml(item);
            Cookies = result.Cookie;
            retCode = result.Html;
            param = Params(retCode);
            try
            {
               item = new HttpItem()                //[size=5]代理不可用时,提示:未将对象引用 到实例!!错误一。本地正常。代理可用时正常。[/size]
                {
                    URL = "http://www.sqee.cn/misc.php?mod=secqaa&action=update&idhash=q" + param[6].Substring(1) + "&0.2664431149100278",
                    Method = "GET",
                    Referer = "http://www.sqee.cn/member.php?mod=register",
                    Cookie = Cookies,
                    ProxyIp = proxyip,
                };
                result = http.GetHtml(item);
                Cookies += result.Cookie;
                retCode = result.Html;
                gifAnser = GetAnser(Utilities.GetMidStr(retCode, "patronage/images/y-", "."));
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
            
            item = new HttpItem()
            {
                URL = "http://www.sqee.cn/misc.php?mod=seccode&action=update&idhash=" + param[6] + "&0.4454971277934601&modid=member::register",
                Method = "GET",
                Referer = "http://www.sqee.cn/member.php?mod=register",
                Cookie = Cookies,
                ProxyIp = proxyip
            };
            result = http.GetHtml(item);
            Cookies += result.Cookie;
            retCode = result.Html;
            update = Utilities.GetMidStr(retCode, "&update=", "&");
            item = new HttpItem()
            {
                URL = "http://www.sqee.cn/misc.php?mod=seccode&update=" + update + "&idhash=" + param[6],
                Method = "GET",
                Referer = "http://www.sqee.cn/member.php?mod=register",
                Cookie = Cookies,
                ProxyIp = proxyip,                
                ResultType = ResultType.Byte
            };
            result = http.GetHtml(item);
            Cookies += result.Cookie;
            if (result.ResultByte.Length < 8000) return;
            pictureBox1.Image = Utilities.GetImageFromByte(result.ResultByte);//获取验证码[size=6]  错误二,本地正常,代理正常时正常,代理不可用时,显示对像NULL[/size]
            //textBox1.Text = result.ResultByte.Length.ToString();
            //if (result.ResultByte.Length > 8000)
            //{
            //    pictureBox1.Image = Utilities.GetImageFromByte(result.ResultByte);//获取验证码
            //}
            //else MessageBox.Show("获取验证码错误,代理有可能不能使用!","提醒");
        }





1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-4-18 08:29:45 | 显示全部楼层
代理不可用是肯定会出错的呀,你还想怎样
 楼主| 发表于 2014-4-18 09:47:42 | 显示全部楼层
程序崩溃呀,下面没法用啦。
//代理不可用时,提示:未将对象引用 到实例!!错误一。本地正常。代理可用时正常。
//获取验证码  错误二,本地正常,代理正常时正常,代理不可用时,显示对像NULL
发表于 2014-4-18 10:17:15 | 显示全部楼层
qq6227575 发表于 2014-4-18 09:47
程序崩溃呀,下面没法用啦。
//代理不可用时,提示:未将对象引用 到实例!!错误一。本地正常。代理可用 ...

处理下异常就是了,异常信息是要处理的,我不可能给你们所人的都处理掉
 楼主| 发表于 2014-4-18 10:27:02 | 显示全部楼层
用try语句没用啊。
发表于 2014-4-18 10:33:11 | 显示全部楼层
你说的是这个啊,这个我已经做过处理了,只要是为NUll 说明是不可用,这个是我做的统一返回提示。未将对象引用 到实例就是因为是Null的问题。你直接判断一下对象是否为Null不就可以做处理了。只要出现错误是肯定不能用的代理。
[C#] 纯文本查看 复制代码
 catch (WebException ex)
            {
                //这里是在发生异常时返回的错误信息
                response = (HttpWebResponse)ex.Response;
                result.Html = ex.Message;
                if (response != null)
                {
                    result.StatusCode = response.StatusCode;
                    result.StatusDescription = response.StatusDescription;
                }
            }
            catch (Exception ex)
            {
                result.Html = ex.Message;
            }


这是我源码里做的处理,你可以根据这个做判断。response !=null 或者是根据result.Html 都可以做出提示
result.Html = ex.Message;并且我都将错误信息显示到了Html里面。
 楼主| 发表于 2014-4-18 10:36:11 | 显示全部楼层
感谢苏飞,菜鸟刚学。不过我这个注册机,前几天能用,不能用的IP也不报错,程序响应慢一点,现在不行,IP不行程序就崩溃了。
发表于 2014-4-18 10:37:48 | 显示全部楼层
qq6227575 发表于 2014-4-18 10:36
感谢苏飞,菜鸟刚学。不过我这个注册机,前几天能用,不能用的IP也不报错,程序响应慢一点,现在不行,IP不 ...

根据我上面的处理做处判断吧,这个异常我是在内部处理过了,你应该只需要判断一下就行了。response !=null 或者是根据result.Html
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-25 20:43

© 2014-2021

快速回复 返回顶部 返回列表