表格提交问题

时间:2014-09-19 14:22:13

标签: c# webrequest

我正在尝试欺骗网页表单,以便我可以通过C#程序而不是网站输入数据。如果我知道表单内容,我认为下面的代码可以工作,但它没有(服务器端数据似乎没有改变)。如果我将其保存为网页并在浏览器中运行它,这样可以正常工作。

<form action="subDOB" method="POST" class="log">
<input type="hidden" name="id" value="27">
<textarea class="logarea" rows="15" name="birth" spellcheck=FALSE>2/27/89
</textarea><br/><br/>
<input class="btn btn-inverse" type="submit" value="Edit info">
</form>

我的代码(在一个按钮中):

WebRequest req = WebRequest.Create("https://site.com/subDOB");//not a real site name :)
        string postData = @"id=27&birth=4/01/88";

        byte[] send = Encoding.Default.GetBytes(postData);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = send.Length;

        Stream sout = req.GetRequestStream();
        sout.Write(send, 0, send.Length);
        sout.Flush();
        sout.Close();

        WebResponse res = req.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream());
        string returnvalue = sr.ReadToEnd();
        rtb.AppendText(returnvalue);// the return value is (apparently) the entire contents of the start page that I had to go through to get to the form

Fiddler返回标题(来自网站):

HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Sun, 21 Sep 2014 15:57:21 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.16
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Page-Speed: 1.8.31.2-3973
Cache-Control: max-age=0, no-cache, no-store
CF-RAY: 16d7753fae4a084a-IAD
Content-Encoding: gzip

Fiddler返回标题(来自应用程序):

HTTP/1.1 302 Moved Temporarily
Server: cloudflare-nginx
Date: Sun, 21 Sep 2014 16:01:57 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d389a08fb9dda3009497a2a19a4e1d6d21411315317326; expires=Mon, 23-Dec-2019     23:50:00 GMT; path=/; domain=.hackerexperience.com; HttpOnly
X-Powered-By: PHP/5.5.16
Set-Cookie: PHPSESSID=fkgs1gdb2n329um5qio2pmf086; path=/; domain=.hackerexperience.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: index
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
CF-RAY: 16d77bfd415b084a-IAD

0 个答案:

没有答案