大家好,我面临一个让我难以忍受一个小时的问题。我正在尝试创建一个应用程序来登录我的工作时间表,所以我不必打开我的网络浏览器并检查,因为我的工作时间变化如此之快。我登录时遇到了一些问题;目前我正在使用webclient尝试使用以下类:
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace myTLC
{
public class TLC
{
private String employeeID;
private String password;
private String employeeName;
private List<String> values = new List<String>();
public TLC(String ID, String password)
{
this.employeeID = ID;
this.password = password;
}
public String TLC_Login()
{
LoadValues();
using (var client = new CookieAwareWebClient())
{
client.Headers.Add(System.Net.HttpRequestHeader.Accept, "text/html, application/xhtml+xml, */*");
client.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip, deflate");
client.Headers.Add(System.Net.HttpRequestHeader.AcceptLanguage, "en-US");
client.Headers.Add(System.Net.HttpRequestHeader.Referer, "https://mytlc.bestbuy.com/etm/");
client.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
client.Headers.Add(System.Net.HttpRequestHeader.CacheControl, "no-cache");
var parm = new NameValueCollection
{
{"wbat", values[0] },
{"pageAction", "login" },
{ "url_login_token", values[1]},
{"login", employeeID },
{"password", password },
{"localeSelected", "false" },
{"wbXpos", "0" },
{"wbYpos", "0" }
};
byte[] response_array = client.UploadValues("https://mytlc.bestbuy.com/etm/login.jsp", parm);
return Encoding.UTF8.GetString(response_array);
}
}
public void LoadValues()
{
using (var client = new CookieAwareWebClient())
{
String pageSource = client.DownloadString("https://mytlc.bestbuy.com/etm/");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(pageSource);
if (doc.ParseErrors != null && doc.ParseErrors.Count() > 0)
{
//Error Handling - I'll add it later on.
}
String wbat = doc.DocumentNode.SelectSingleNode(@"//input[@id='wbat']").GetAttributeValue("value", "null");
String url_token = doc.DocumentNode.SelectSingleNode(@"//*[@id='inforLoginWrappingDiv']/input").GetAttributeValue("value", "null");
values.Add(wbat);
values.Add(url_token);
}
}
public String e_ID
{
get { return employeeID; }
set { employeeID = value; }
}
public String e_Pass
{
get { return password; }
set { password = value; }
}
public String e_Name
{
get { return employeeName; }
set { employeeName = value; }
}
}
}
请求数据(由Google Chrome处理并在Fiddler中检查)如下所示:
POST https://mytlc.bestbuy.com/etm/login.jsp HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: https://mytlc.bestbuy.com/etm/
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: mytlc.bestbuy.com
Content-Length: 167
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=0000HLkYnJDHq9wQwU8qPA90MYq:182bi51ql
wbat=yP6uE%2F7vW9xhvIPEm1Ro6h%2FQNEObayZ6&pageAction=login&url_login_token=-1959600643775&login=xxxx&password=xxxx&localeSelected=false&wbXpos=0&wbYpos=0
(登录令牌中的某些数字被遗漏,用户名和通行证被遗漏,显然有充分理由)
我能解决的问题是什么?我通过WebClient
类处理我的请求(StackOverflow上的某个人之前已发布过这些请求,归功于他们)我抓住了我需要正确传递的所有值;我已确保所有值都被正确拉出,并且我在登录尝试中没有出现任何错误,所有程序都会重定向到登录页面而没有任何错误消息。
我在请求人员中遗漏了什么,或者我在这里完全失明了?
我不认为它与.jsp有什么关系?这意味着他们正在使用Java,因此原始的WebRequest无法工作,因为它们无法通过Java / Javascript?这是我唯一的想法,为什么它不起作用。