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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 3602|回复: 3

[HttpHelper] 为何Post数据无响应,Post空数据则有响应,附代码。

[复制链接]
发表于 2015-3-24 11:40:07 | 显示全部楼层 |阅读模式
5金钱
想模拟登录的地址:http://fanxing.kugou.com/index.php?action=msgBox
账号密码随意输入,只要能拿到响应。
根据抓包内容,得到如下代码。但是POST参数有值的话,就会拿不到响应。

    public class MockRequest
    {
        private string url = "http://fanxing.kugou.com/Services.php?act=UserService.UserService&mtd=login";
        private string referer = "http://fanxing.kugou.com/index.php?action=msgBox";
        private string param = "args=[\"123\",\"123\",\"\" ,\"0\"]&test=";
        private string contentType = "application/x-www-form-urlencoded";
        private string userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
        private string host = "fanxing.kugou.com";

        byte[] data;

        private CookieContainer theCC = new CookieContainer();

        public MockRequest()
        {
            //使用请求参数 得不到相应
            data = Encoding.UTF8.GetBytes(param);

            //如果无请求参数 可以得到响应
            //data = new byte[] { };
        }

        public void Run()
        {
            LoginByRequest();
            //LoginByWebClient();
        }

        private void LoginByRequest()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Accept = "*/*";
            request.ContentType = contentType;
            request.Referer = referer;

            request.ContentLength = data.Length;
            request.UserAgent = userAgent;

            request.Host = host;
            request.KeepAlive = true;
            request.Headers["Accept-Encoding"] = "gzip, deflate";
            request.Headers["Accept-Language"] = "zh-CN";

            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            request.CookieContainer = theCC;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            theCC.Add(response.Cookies);
            Stream stream = response.GetResponseStream();
            string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
        }

        private void LoginByWebClient()
        {
            WebClient wc = new WebClient();

            wc.Headers.Add("Content-Type", contentType);
            wc.Headers.Add("ContentLength", data.Length.ToString());

            byte[] recData = wc.UploadData(url, "POST", data);

            string result = Encoding.UTF8.GetString(recData);
        }
    }


最佳答案

查看完整内容

这个是Http协议版本的问题,直接用Httphelper万能框架可实现 [mw_shl_code=csharp,true] HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://fanxing.kugou.com/Services.php?act=UserService.UserService&mtd=login",//URL 必需项 Method = "post",//URL 可选项 默认为Get Cookie ...


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-3-24 11:40:08 | 显示全部楼层
这个是Http协议版本的问题,直接用Httphelper万能框架可实现

[C#] 纯文本查看 复制代码
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "http://fanxing.kugou.com/Services.php?act=UserService.UserService&mtd=login",//URL     必需项    
                Method = "post",//URL     可选项 默认为Get   
                Cookie = "GameLastLogin=487259631",//字符串Cookie     可选项   
                Referer = "http://fanxing.kugou.com/index.php?action=msgBox",//来源URL     可选项   
                Postdata = "args=[\"sufei\",\"123456\",\"\" ,\"0\"]&test=",//Post数据     可选项GET时不需要写   
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
            };
            item.ProtocolVersion = HttpVersion.Version10;
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            string cookie = result.Cookie;


账户密码我乱写的,改成自己的就行了
回复

使用道具 举报

发表于 2015-3-24 12:00:19 | 显示全部楼层
POST /Services.php?act=UserService.UserService&mtd=login HTTP/1.1

现这种情况要可以加一句item.ProtocolVersion = HttpVersion.Version10;
回复

使用道具 举报

 楼主| 发表于 2015-3-24 12:11:51 | 显示全部楼层
可以了。多谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-4-30 21:24

© 2014-2021

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