设置正确的post数据格式
2014-02-26 09:56
267 查看
之前一直使用苏飞的HttpHelper类来访问网络,用起来一直感觉很爽。使用其工具直接生成访问代码很是方便。直到昨天下午做到需要使用wpf来post两个字段数据到服务器,服务器使用ASP.NET MVC来接收表单数据时出现了问题。
首先:按照正常使用习惯来生成的代码是:
服务器来接受到请求,但是接受到的username 和password字段都是null。
小猪捣鼓了一个下午一直认为是post数据的问题。尝试将其改成
甚至是
问题依旧啊!害得我昨天晚上一生气玩dota到12点~:-)
期间自己编写html代码来测试数据
这样测试是能够接受到数据的。各种百度、谷歌、官方论坛找到下列结果http://www.sufeinet.com/forum.php?mod=viewthread&tid=7497&highlight=post%2B%B1%ED%B5%A5
原来需要将请求的ContenType,这样HttpWebRequest类会将post数据当成表单数据来处理,这样在ASP.NET MVC后台就可以成功接受到数据。
首先:按照正常使用习惯来生成的代码是:
HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://localhost:2250/api/login",//URL 必需项 Method = "post",//URL 可选项 默认为Get IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写 Cookie = "",//字符串Cookie 可选项 Referer ="",//来源URL 可选项 Postdata = "username=1@1.com&password=123456",//Post数据 可选项GET时不需要写 Timeout = 100000,//连接超时时间 可选项默认为100000 ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值 ContentType = "text/html",//返回类型 可选项有默认值 Allowautoredirect = false,//是否根据301跳转 可选项 //CerPath = "d:\123.cer",//证书绝对路径 可选项不需要证书时可以不写这个参数 //Connectionlimit = 1024,//最大连接数 可选项 默认为1024 ProxyIp = "",//代理服务器ID 可选项 不需要代理 时可以不设置这三个参数 //ProxyPwd = "123456",//代理服务器密码 可选项 //ProxyUserName = "administrator",//代理服务器账户名 可选项 }; HttpResult result = http.GetHtml(item); string html = result.Html; string cookie = result.Cookie;
服务器来接受到请求,但是接受到的username 和password字段都是null。
小猪捣鼓了一个下午一直认为是post数据的问题。尝试将其改成
string postData = "{username=123&password=123}"
甚至是
string postData = "{username='123'&password='123'}"
问题依旧啊!害得我昨天晚上一生气玩dota到12点~:-)
期间自己编写html代码来测试数据
<html> <head> </head> <body> <div> <form action="http://localhost:2250/api/login" method ="Post"> <input type = "text" name = "username"/> <input type = "password" name = "password"/> <input type = "submit" > </form> </div> </body> </html>
这样测试是能够接受到数据的。各种百度、谷歌、官方论坛找到下列结果http://www.sufeinet.com/forum.php?mod=viewthread&tid=7497&highlight=post%2B%B1%ED%B5%A5
原来需要将请求的ContenType,这样HttpWebRequest类会将post数据当成表单数据来处理,这样在ASP.NET MVC后台就可以成功接受到数据。
ContentType = "application/x-www-form-urlencoded",
相关文章推荐
- Epplus 操作Excel 2007/2010
- java.io输入流怎么转成输出流
- CentOS 6.5编译安装MySQL 5.6.16
- SQLServer建立架构(模式/目录)_建立表_建立视图
- ios--安全攻防01--Hack必备的命令与工具
- 如何设置win7 锁屏不断网
- Android JNI知识简介
- 解决android-support-v4.jar引用外部项目冲突问题
- C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)
- Epplus 操作Excel 2007/2010
- 【机器学习系列】EM算法
- 关于maven的pom文件配置问题
- 重启服务器后网站访问不了,防火墙在作怪
- 一个简单的又好看的验证码
- IOS - rangeOfString、NSNotFound
- 程序员找工作渠道
- oracle 常用命令
- onclick事件分析
- Java动态(后期)绑定和overloading 向上转型
- Jrebel 的配置及使用