首页 新闻 会员 周边 捐助

模拟按钮的post请求进行提交,老错?

0
悬赏园豆:5 [已关闭问题] 关闭于 2011-09-26 10:41

 比照着网上提供的一些代码:自己写了一下,但是老是出现错误:无法发送具有此谓词类型的内容正文。一直提示这个错误,那位高手看一下我的代码那里有问题,求指导!

string url = "http://localhost:30747/WebForm1.aspx";
            string __VIEWSTATE = "/wEPDwUKLTg0OTIzODQwNWRkQrsiVjjfcttD1psiP9pESun/JH4=";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            string postData = "__VIEWSTATE=" + __VIEWSTATE + "&txt1=admin&txt2=admin&btnOK=ok";
            byte[] b = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = b.Length;
            Stream stream = request.GetRequestStream();

            stream.Write(b, 0, b.Length);

Impossible的主页 Impossible | 初学一级 | 园豆:72
提问于:2011-09-17 15:10
< >
分享
所有回答(2)
0

改成这样试试:

string url ="http://localhost:30747/WebForm1.aspx";
string __VIEWSTATE ="/wEPDwUKLTg0OTIzODQwNWRkQrsiVjjfcttD1psiP9pESun/JH4=";
HttpWebRequest request
= (HttpWebRequest)WebRequest.Create(url);

request.Method
="POST";
request.ContentType
="application/x-www-form-urlencoded";
string postData ="__VIEWSTATE="+ __VIEWSTATE +"&txt1=admin&txt2=admin&btnOK=ok";
byte[] b = Encoding.UTF8.GetBytes(postData);
request.ContentLength
= b.Length;

HttpWebResponse response
= request.GetResponse() as HttpWebResponse;
Stream stream
= request.GetRequestStream();

stream.Write(b,
0, b.Length);
artwl | 园豆:16736 (专家六级) | 2011-09-18 20:00
0

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

不妨把上面的这行代码放到下面的这行代码后面试试:

stream.Write(b, 0, b.Length);

I,Robot | 园豆:9783 (大侠五级) | 2011-09-19 02:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册