如何以编程方式登录到网站screenscape? [英] How to programmatically log in to a website to screenscape?

查看:102
本文介绍了如何以编程方式登录到网站screenscape?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个网站,这不是我的,为了得到这个信息,我需要登录到该网站来收集信息的一些信息,这种情况通过一个HTML表单。我怎样才能做到这一点验证screenscaping在C#?

I need some information from a website that's not mine, in order to get this information I need to login to the website to gather the information, this happens through a HTML form. How can I do this authenticated screenscaping in C#?

的额外信息:


  • 基于Cookie的身份验证。

  • POST操作需要。

推荐答案

您会提出请求,就好像你刚刚填写的表单。假设它的POST例如,你与正确的数据的POST请求。现在,如果你不能直接登录到要刮在同一个页面,你将不得不跟踪任何Cookie是你的登录请求后设置的,包括他们在您的拼抢要求,让您保持登录。

You'd make the request as though you'd just filled out the form. Assuming it's POST for example, you make a POST request with the correct data. Now if you can't login directly to the same page you want to scrape, you will have to track whatever cookies are set after your login request, and include them in your scraping request to allow you to stay logged in.

这可能看起来像:

HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
string postData="FormNameForUserId=" + strUserId + "&FormNameForPassword=" + strPassword;
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
    postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
// Probably want to inspect the http.Headers here first
http = WebRequest.Create(url2) as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;

也许吧。

这篇关于如何以编程方式登录到网站screenscape?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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