我一直在使用HttpWebRequest来获取Cookie并抓取所需文档的网站。我一直在加载网站,然后在没有太多麻烦的情况下将它们分解为ViewState。然后我拍了一下。
Indiana WOTC website使用一个隐藏字段作为回邮的一部分。该字段名为“ lt”,并随页面随机生成。我无法加载页面并获取代码,因为当我访问页面以输入帖子数据时,代码会更改。
这是我的cookie获取代码;
private CookieContainer GetCookies(string Login_URL, List<Post_Parameters> parameters)
{
HttpWebRequest webRequest = WebRequest.Create(Login_URL) as HttpWebRequest;
string responseData = "";
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
responseData = responseReader.ReadToEnd();
}
List<string> ParameterSet = new List<string>();
foreach (Post_Parameters item in parameters)
{
if (item.Value == "???")
{
ParameterSet.Add(item.Attribute.Trim('"') + "=" + Extract(responseData, item.Attribute));
}
else
{
ParameterSet.Add(item.Attribute.Trim('"') + "=" + item.Value);
}
}
string postData = string.Join("&", ParameterSet);
CookieContainer cookies = new CookieContainer();
webRequest = WebRequest.Create(Login_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;
using (StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter.Write(postData);
}
//Debug Logins
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
// Console.Write(responseReader.ReadToEnd());
}
return cookies;
}
private string Extract(string s, string d)
{
string viewStateNameDelimiter = d;
string valueDelimiter = "value=\"";
int viewStateNamePosition = s.IndexOf(viewStateNameDelimiter);
int viewStateValuePosition = s.IndexOf(valueDelimiter, viewStateNamePosition);
int viewStateStartPosition = viewStateValuePosition + valueDelimiter.Length;
int viewStateEndPosition = s.IndexOf("\"", viewStateStartPosition);
string x = "";
x = WebUtility.UrlEncode(s.Substring(viewStateStartPosition, viewStateEndPosition - viewStateStartPosition));
return x;
}
private struct Post_Parameters
{
public string Attribute { get; set; }
public string Value { get; set; }
}
这是我通常使用的方式;
string LOGIN_URL = "https://wotc.jfs.ohio.gov/UserLogin.aspx?ReturnUrl=%2f";
List<Post_Parameters> x = new List<Post_Parameters>();
x.Add(new Post_Parameters { Attribute = "__VIEWSTATEGENERATOR", Value = "???" });
x.Add(new Post_Parameters { Attribute = "__EVENTVALIDATION", Value = "???" });
x.Add(new Post_Parameters { Attribute = "__VIEWSTATE", Value = "???" });
x.Add(new Post_Parameters { Attribute = "ctl00%24ContentPlaceHolder1%24Login1%24Fein", Value = "*****" });
x.Add(new Post_Parameters { Attribute = "ctl00%24ContentPlaceHolder1%24Login1%24UserId", Value = "*****" });
x.Add(new Post_Parameters { Attribute = "ctl00%24ContentPlaceHolder1%24Login1%24Password", Value = "*****" });
x.Add(new Post_Parameters { Attribute = "ctl00%24ContentPlaceHolder1%24Login1%24LoginButton", Value = "Log+In" });
x.Add(new Post_Parameters { Attribute = "ctl00_ContentPlaceHolder1_rlvBreakingNews_ClientState", Value = "" });
var i = GetCookies(LOGIN_URL, x);
这是我在印第安纳州网站上使用它的方式;
string LOGIN_URL = "https://webapps.dwd.in.gov/DWDAuthentication/login?service=https%3A%2F%2Fwebapps.dwd.in.gov%2FeWotc%2Fdefault-flow.htm";
List<Post_Parameters> x = new List<Post_Parameters>();
x.Add(new Post_Parameters { Attribute = "username", Value = "*****" });
x.Add(new Post_Parameters { Attribute = "password", Value = "*****" });
x.Add(new Post_Parameters { Attribute = "_eventId_signIn", Value = "Sign+In" });
x.Add(new Post_Parameters { Attribute = "\"lt", Value = "Some How Post the Hidden Field from the Page" });
x.Add(new Post_Parameters { Attribute = "execution", Value = "???" });
var i = TestGetCookies(LOGIN_URL, x);
基本上,我需要以某种方式将页面的隐藏字段发送到POST。我认为这是不可能的,因为我正在用POST数据加载页面。
在不使用WebBrowser控件的情况下是否有可能?如果是这样,我将如何开始工作?