苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 5452|回复: 6

[HttpHelper] 貌似发现了HttpHelper的一个bug,或者是我使用错误!最终我是这样解决的!

[复制链接]
发表于 2016-6-28 22:58:25 | 显示全部楼层 |阅读模式
我抓“百姓网安卓app”的请求头是这样的
POST http://www.baixing.com/api/mobile/user.login/ HTTP/1.1
BAPI-USER-TOKEN:
udid: 382b07c277a4dcb8
APP-VERSION: 7.5.0
BAPI-APP-KEY: api_androidbaixing
BAPI-NONCE: 382b07c277a4dcb8
BAPI-HASH: 31713c34ace51df794b39e288ba87707
User-Agent: com.quanleimu.activity/7.5.0;OPPO;OPPO;OPPO+A53;5.1.1;720x1200;
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip
Host: www.baixing.com
Content-Length: 62
Connection: Keep-Alive


{"password":"347723","type":"mobile","identity":"18312854037"}



我用HttpHelper写的代码如下:
[C#] 纯文本查看 复制代码
 private void button1_Click(object sender, EventArgs e)
        {
            HttpResult hr = null;
            HttpHelper hh = new HttpHelper();
            HttpItem hi = new HttpItem();
            hi.URL = "http://www.baixing.com/api/mobile/user.login/";
            hi.KeepAlive = true;
            hi.UserAgent = "com.quanleimu.activity/7.5.0;OPPO;OPPO;OPPO+A53;5.1.1;720x1200;";
            hi.Header.Add("BAPI-USER-TOKEN", "");
            hi.Header.Add("udid", "382b07c277a4dcb8");
            hi.Header.Add("APP-VERSION", "7.5.0");
            hi.Header.Add("BAPI-APP-KEY", "api_androidbaixing");
            hi.Header.Add("BAPI-NONCE", "382b07c277a4dcb8");
            hi.Header.Add("BAPI-HASH", "31713c34ace51df794b39e288ba87707");
            hi.ContentType = "application/json; charset=utf-8";
            // hi.Postdata = @"{""password"":""347723"",""type"":""mobile"",""identity"":""18312854037""}";
            string body = @"{""password"":""347723"",""type"":""mobile"",""identity"":""18312854037""}";
            byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
            hi.Header.Set(HttpRequestHeader.AcceptEncoding, "gzip");
            hi.PostdataByte = postBytes;
            hi.Timeout = 100000;
            hi.ReadWriteTimeout = 30000;
            hr = hh.GetHtml(hi);
            string s = hr.Html;

        }

得到的返回数据是  {"error":2007,"message":"","ext":null}
但是最终用下面的代码(参考了HttpHepler.cs的源代码)才得到正确的返回数据
[C#] 纯文本查看 复制代码
 private void button1_Click(object sender, EventArgs e)
        {
            MakeRequests();
        }
        HttpWebResponse response = null;
        private void MakeRequests()
        {
           
           
            if (Request_www_baixing_com(out response))
            {
                byte[] ResponseByte = GetByte();
                if (ResponseByte != null && ResponseByte.Length > 0)
                {
                    string str = encoding.GetString(ResponseByte);                //@@@看过来@@@最终得到的返回数据,但是格式有点问题,不过最够判断登陆成功了
                }
                //  response.Close();
            }
        }
        
        private MemoryStream GetMemoryStream(Stream streamResponse)
        {
            MemoryStream _stream = new MemoryStream();
            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = streamResponse.Read(buffer, 0, Length);
            while (bytesRead > 0)
            {
                _stream.Write(buffer, 0, bytesRead);
                bytesRead = streamResponse.Read(buffer, 0, Length);
            }
            return _stream;
        }
        private bool Request_www_baixing_com(out HttpWebResponse response)
        {
            response = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baixing.com/api/mobile/user.login/");

                request.Headers.Add("BAPI-USER-TOKEN", @"");
                request.Headers.Add("udid", @"382b07c277a4dcb8");
                request.Headers.Add("APP-VERSION", @"7.5.0");
                request.Headers.Add("BAPI-APP-KEY", @"api_androidbaixing");
                request.Headers.Add("BAPI-NONCE", @"382b07c277a4dcb8");
                request.Headers.Add("BAPI-HASH", @"31713c34ace51df794b39e288ba87707");
                request.UserAgent = "com.quanleimu.activity/7.5.0;OPPO;OPPO;OPPO+A53;5.1.1;720x1200;";
                request.ContentType = "application/json; charset=utf-8";
                request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip");

                request.Method = "POST";
                request.ServicePoint.Expect100Continue = false;

                string body = @"{""password"":""347723"",""type"":""mobile"",""identity"":""18312854037""}";
                byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
                request.ContentLength = postBytes.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(postBytes, 0, postBytes.Length);
                stream.Close();

                response = (HttpWebResponse)request.GetResponse();
               // string ss = response.
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
                else return false;
            }
            catch (Exception)
            {
                if (response != null) response.Close();
                return false;
            }

            return true;

        }
        private byte[] GetByte()
        {
            byte[] ResponseByte = null;
            MemoryStream _stream = new MemoryStream();

          //  GZIIP处理
            if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
            {
                //开始读取流并设置编码方式
                _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
            }
            else
            {
                //开始读取流并设置编码方式
                _stream = GetMemoryStream(response.GetResponseStream());
            }
          //  获取Byte
            ResponseByte = _stream.ToArray();
            _stream.Close();
            return ResponseByte;
        }
        private Encoding encoding = Encoding.Default;

请问飞哥,这到底是什么问题!


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2016-6-29 08:14:48 | 显示全部楼层
你上下的代码差别很大啊,你应该在Htttphelper里也使用同样的参数,同样的配置,
 楼主| 发表于 2016-6-29 09:15:06 | 显示全部楼层
站长苏飞 发表于 2016-6-29 08:14
你上下的代码差别很大啊,你应该在Htttphelper里也使用同样的参数,同样的配置,

下面的代码是使用fiddler code生成的,是底层的代码,使用了同样的的参数,单数出来的结果不一样啊!
 楼主| 发表于 2016-6-29 11:08:24 | 显示全部楼层
使用fiddler code自己生成的代码是可以正常请求的
下面是fiddler生成的代码
[C#] 纯文本查看 复制代码
private void MakeRequests()
{
    HttpWebResponse response;
 
    if (Request_www_baixing_com(out response))
    {
        response.Close();
    }
}
 
private bool Request_www_baixing_com(out HttpWebResponse response)
{
    response = null;
 
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baixing.com/api/mobile/user.login/");
 
        request.Headers.Add("BAPI-USER-TOKEN", @"");
        request.Headers.Add("udid", @"382b07c277a4dcb8");
        request.Headers.Add("APP-VERSION", @"7.5.0");
        request.Headers.Add("BAPI-APP-KEY", @"api_androidbaixing");
        request.Headers.Add("BAPI-NONCE", @"382b07c277a4dcb8");
        request.Headers.Add("BAPI-HASH", @"31713c34ace51df794b39e288ba87707");
        request.UserAgent = "com.quanleimu.activity/7.5.0;OPPO;OPPO;OPPO+A53;5.1.1;720x1200;";
        request.ContentType = "application/json; charset=utf-8";
        request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip");
 
        request.Method = "POST";
        request.ServicePoint.Expect100Continue = false;
 
        string body = @"{""password"":""347723"",""type"":""mobile"",""identity"":""18312854037""}";
        byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
        request.ContentLength = postBytes.Length;
        Stream stream = request.GetRequestStream();
        stream.Write(postBytes, 0, postBytes.Length);
        stream.Close();
 
        response = (HttpWebResponse)request.GetResponse();
    }
    catch (WebException e)
    {
        if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
        else return false;
    }
    catch (Exception)
    {
        if(response != null) response.Close();
        return false;
    }
 
    return true;
}

我抓包得到的返回值是
{"result":{"user":{"id":"u97293616","name":"用户_88230919","allowChat":true,"createdTime":"1368970340","mobile":"18312854037","image":"http://img5.baixing.net/4631eb6a494ada1ff8c65e00c25f7719.png_180x180","description":"","gender":null,"balance":"0","adPostedCount":0,"adFavedCount":0,"resumeSentCount":0,"resumeReceivedCount":0,"boundAccounts":[],"app_boundAccountsHtml":"","publicReviewer":0,"isDefaultAvatar":true},"token":"ut577228122f1772.95025799"}}

运行fiddler生成的代码,再用fiddler抓包得到的就是这个正常的返回数据!希望老大解决!
 楼主| 发表于 2016-6-29 11:25:39 | 显示全部楼层
问题解决了,是我傻逼了!哎!
发表于 2016-6-29 13:45:08 | 显示全部楼层
request.ServicePoint.Expect100Continue = false;
发表于 2016-7-1 09:41:15 | 显示全部楼层
njjwdy 发表于 2016-6-29 11:08
使用fiddler code自己生成的代码是可以正常请求的
下面是fiddler生成的代码
[mw_shl_code=csharp,true]pr ...

你也在做百姓吗?网页端的你可以搞定吗?我做二手房,做了一办,搞不定了。希望交流下
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2025-1-31 13:57

© 2014-2021

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