简体   繁体   English

C#-以编程方式填写并提交登录

[英]c# - programmatically form fill and submit login

Using C# and ASP.NET I want to programmatically fill in values for a web form and then 'POST' those values. 使用C#和ASP.NET,我想以编程方式填写Web表单的值,然后“发布”这些值。

I've seen examples of others using webclient and other classes in posts such as: 我在帖子中看到了其他使用webclient和其他类的示例,例如:

How do you programmatically fill in a form and 'POST' a web page? 您如何以编程方式填写表格并“发布”网页?

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) Using the Post code in this thread, how can you determine if the Post was a success? 1)使用此线程中的Post代码,如何确定Post是否成功? We cant really presume a 200 response is a success. 我们不能真正假设200个响应是成功的。 It wouldn't necessarily be correct to presume a 302 redirect signifies success either. 假定302重定向也表示成功也不一定正确。 Is there any sure fire way to determine we are logged on successfully after the post? 有什么确定的方法可以确定我们在帖子发布后是否成功登录?

2) Do some sites block requests that do not originate from their own domain? 2)是否某些网站阻止了不是源自其自己域的请求?

yes some sites block request. 是的,某些网站阻止了请求。 but you can check the login with auth cookie. 但是您可以使用身份验证Cookie检查登录名。 Use HttpWebRequest/HttpWebResponse 使用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();

now check response.Cookies for auth cookie 现在检查response.Cookies的身份验证Cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM