C# - 编程形补并提交登录 [英] c# - programmatically form fill and submit login

查看:127
本文介绍了C# - 编程形补并提交登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#和ASP.NET我要在编程值填写网络表格,然后POST的价值观。

Using C# and ASP.NET I want to programmatically fill in values for a web form and then 'POST' those values.

我见过别人使用的职位,如Web客户端和其他类的例子:

I've seen examples of others using webclient and other classes in posts such as:

<一个href=\"http://stackoverflow.com/questions/26857/how-do-you-programmatically-fill-in-a-form-and-post-a-web-page/27232297#27232297\">How你编程的形式和POST网页填写?

我期待开展类似的功能 - 在这里我自动发布的数据形式。就我而言,我希望将数据提交到模拟登录。我有几个问题:

I am looking to carry out similar functionality - where I automatically post data to a form. In my case I am looking to submit data to simulate a login. I have a couple of questions:

1)在此线程使用POST code,你怎么能确定该帖子是成功的?我们不能真正presume 200响应是成功的。这并不一定是正确的presume 302重定向表示成功要么。有没有确保消防方式来确定我们的帖子后成功登录?

1) Using the Post code in this thread, how can you determine if the Post was a success? We cant really presume a 200 response is a success. It wouldn't necessarily be correct to presume a 302 redirect signifies success either. Is there any sure fire way to determine we are logged on successfully after the post?

2)做一些网站阻止不从自己的领域发起请求?

2) Do some sites block requests that do not originate from their own domain?

推荐答案

有一些网站块请求。但你可以检查与身份验证cookie中的登录。使用的HttpWebRequest / HttpWebResponse

yes some sites block request. but you can check the login with auth cookie. Use HttpWebRequest/HttpWebResponse

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));

byte[] data = Encoding.Default.GetBytes("item1=11111&item2=22222&Item3=33333");
request.Method = "POST";
request.ContentLength = data.Length;

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

request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

现在检查 response.Cookies 的AUTH的cookie

now check response.Cookies for auth cookie

这篇关于C# - 编程形补并提交登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆