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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 7494|回复: 15

[其他] 请教关于post

[复制链接]
发表于 2015-5-22 11:23:17 | 显示全部楼层 |阅读模式
       在设置了HttpResponseHeader的ContentType 出现错误

“配置参数时出错:必须使用适当的属性或方法修改此标头”参数名 :name
    WebHeaderCollection head=new WebHeaderCollection();
            head.Add(HttpResponseHeader.ContentType,"application/json;charset=UTF-8");
           
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = txturl.Text,//URL     必需项   
                Method = "post",//URL     可选项 默认为Get   
                ContentType = txtType.Text,//返回类型    可选项有默认值
                PostDataType = PostDataType.String,//默认为字符串,同时支持Byte和文件方法
                PostEncoding = System.Text.Encoding.UTF8,//默认为Default,
                Postdata = txtpostdata.Text,//Post要发送的数据
                Header = head //提交数据类型
            };

            HttpResult result = http.GetHtml(item);
            MessageBox.Show(result.StatusDescription);


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-5-22 11:31:33 | 显示全部楼层
ContentType = "text/html" 这个直接用属性就行了,提示的很明显是属性,不能使用Header来赋值
发表于 2015-5-22 11:39:44 | 显示全部楼层
  HttpItem item = new HttpItem()
            {
                URL = txturl.Text,//URL     必需项   
ContentType = "application/json;charset=UTF-8"
            };
 楼主| 发表于 2015-5-22 11:46:02 | 显示全部楼层
本帖最后由 redcat 于 2015-5-22 11:52 编辑

未解决~
回复

使用道具 举报

 楼主| 发表于 2015-5-22 11:47:25 | 显示全部楼层
站长苏飞 发表于 2015-5-22 11:39
HttpItem item = new HttpItem()
            {
                URL = txturl.Text,//URL     必需项  ...

您说的这个ContentType 是请求返回参数吧。 我设置的是 post的提交参数
发表于 2015-5-22 11:47:51 | 显示全部楼层
/// <summary>
        /// 根据RAW封装请求包
        /// </summary>
        /// <param name="Raw">请求保的RAW信息</param>
        /// <returns>HttpWebRequest请求</returns>
        public HttpWebRequest RawToRequest(string Raw)
        {
            string[] ruris = Raw.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string ruri = ruris[0].Split(' ')[1];

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ruri);
            req.Method = ruris[0].Split(' ')[0];
            string name="";
            string val = "";
            try
            {
                for (int i = 1; i < ruris.Length; i++)
                {
                    string[] nvals = ruris[i].Split(new char[] { ':' }, 2);
                    name = nvals[0].Trim();
                    val = nvals[1].Trim();
                    if (name == "Accept")
                    {
                        req.Accept = val;
                    }
                    else if (name == "Referer")
                    {
                        req.Referer = val;
                    }
                    else if (name == "User-Agent" || name == "UserAgent")
                    {
                        req.UserAgent = val;
                    }
                    else if (name == "ContentType" || name == "Content-Type")
                    {
                        req.ContentType = val;
                    }
                    else if (name == "Connection")
                    {
                        //req.Headers.Add(HttpRequestHeader.Connection, val);
                        if (val == "Keep-Alive"||val=="KeepAlive")
                        {
                            req.KeepAlive = false;
                        }
                        else
                        {
                            req.KeepAlive = true;
                        }
                    }
                    else if (name == "Host")
                    {
                        //
                    }
                    else if (name=="Content-Length"||name=="ContentLength")
                    {
                        //
                    }
                    else
                    {
                        req.Headers.Add(name, val);
                        //req.Headers.Add("Origin", "login.tianji.com");
                        //req.Headers.Add("X-Requested-With", "XMLHttpRequest");
                        //req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
                        //req.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
            return req;
        }

//你用这段代码基本上可以封装一个HttpWebRequest 包了。Raw值你自己用从fiddle里面复制出来就行了。
发表于 2015-5-22 11:52:05 | 显示全部楼层
redcat 发表于 2015-5-22 11:47
您说的这个ContentType 是请求返回参数吧。 我设置的是 post的提交参数

当然是上传的,总共就这一个,只要写一行就行了
[C#] 纯文本查看 复制代码
HttpItem item = new HttpItem()
            {
                URL = txturl.Text,//URL     必需项    
ContentType = "application/json;charset=UTF-8"
            };


返回的参数你设置有什么用?返回是人家定的不是你来定的。

反回的如果是你可以修改的,那还请求什么,直接自己造一个不就行了,这从逻辑上都说不通的

 楼主| 发表于 2015-5-22 11:58:52 | 显示全部楼层
站长苏飞 发表于 2015-5-22 11:52
当然是上传的,总共就这一个,只要写一行就行了
[mw_shl_code=csharp,true]HttpItem item = new HttpIte ...

哦 明白了~不好意思~~~但是现在出现了。 504 Gateway Time-out 错误 不知道怎么解决~~~~我用postman工具提交就是正常的。
发表于 2015-5-22 12:01:34 | 显示全部楼层
这个原因就多了,可能是Cookie问题,也有可能是其他参数限制,具体的得看一下详细的抓包,做下对比看看
最好是不要少参数之类的
 楼主| 发表于 2015-5-22 12:59:01 | 显示全部楼层
站长苏飞 发表于 2015-5-22 12:01
这个原因就多了,可能是Cookie问题,也有可能是其他参数限制,具体的得看一下详细的抓包,做下对比看看
最 ...

测试后发现是cookie的问题,

但是设置了AutoRedirectCookie=true后 一样得不到正确的。

AutoRedirectCookie=true,
Allowautoredirect=true,
MaximumAutomaticRedirections=1000,

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

本版积分规则

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

GMT+8, 2024-5-5 09:03

© 2014-2021

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