[英]Using JSOUP to Login to ConEd website
我已經廣泛閱讀了有關此操作的信息,並嘗試了許多不同的變體,但無法使其正常工作。
基本上,我只想登錄ConEdison網站並抓取我的帳單記錄。 這是我所擁有的:
Connection.Response loginForm = Jsoup.connect("https://apps.coned.com/cemyaccount/NonMemberPages/Login.aspx?lang=eng")
.data("_LASTFOCUS","")
.data("_EVENTTARGET","")
.data("_EVENTARGUMENT","")
.data("_VIEWSTATE", viewState)
.data("_EVENTVALIDATION", eventValidation)
.data("ctl00$Main$Login1$UserName", username)
.data("ctl00$Main$Login1$Password", password)
.data("ctl00$Main$Login1$LoginButton", "Sign In")
.userAgent("Mozilla/5.0")
.method(Method.POST)
.execute();
Map<String, String> loginCookies = loginForm.cookies();
Document document = Jsoup.connect("https://apps.coned.com/CEMyAccount/CSOL/BillHistory.aspx?lang=eng")
.cookies(loginCookies)
.get();
Elements data = document.select("table.ctl00_Main_lvBillHistory_Table1");
//checking if it found the right page
System.out.println("document: " + document);
//checking if it found the table
System.out.println("data: " + data);
我知道信息是正確的(盡管我不知道是否真的需要傳遞沒有值的數據參數)。
我沒有任何錯誤,只是打印了登錄頁面( https://apps.coned.com/cemyaccount/NonMemberPages/Login.aspx?lang=eng )
任何幫助將不勝感激。
謝謝
編輯
因此,我現在確信我無法進入內部頁面,因為在POST到https://apps.coned.com/cemyaccount/NonMemberPages/Login.aspx?lang=eng之后 ,設置了3個cookie,但隨后它將GET請求發送到https://apps.coned.com/cemyaccount/SessionTransfer.aspx?dir=2asp&url=https://apps.coned.com/csol/MainHome.asp?src=DOTNET,然后發送到https: //apps.coned.com/csol/SessionTransfer.asp?dir=2asp&guid=3c413f48-d2eb-434a-896b-f9c4eb100714&url=https://apps.coned.com/csol/MainHome.asp?src=DOTNET&frm=進入首頁之前的Cookie
有誰知道我如何遵循所有這些重定向並最終獲得cookie?
這是我目前擁有的,但是我無法從POST調用中獲取Cookie。
Response response = Jsoup
.connect("https://apps.coned.com/cemyaccount/NonMemberPages/Login.aspx?lang=eng")
.method(Method.GET)
.execute();
Map<String, String> cookies = response.cookies();
cookies.put("NSC_DpoFe_Bqqt-TTM-pme", response.cookie("NSC_DpoFe_Bqqt-TTM-ofx"));
System.out.println("response cookies: " + cookies);
response = Jsoup
.connect("https://apps.coned.com/cemyaccount/NonMemberPages/Login.aspx?lang=eng")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
.header("Accept-Encoding", "gzip, deflate")
.header("Accept-Language", "en-US,en;q=0.8")
.header("Connection", "keep-alive")
.cookies(cookies)
.header("Host", "apps.coned.com")
.referrer("https://apps.coned.com/cemyaccount/NonMemberPages/Login.aspx?lang=eng&login=0")
.userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36")
.data("_LASTFOCUS", "")
.data("_EVENTTARGET", "")
.data("_EVENTARGUMENT", "")
.data("_VIEWSTATE", viewState)
.data("_EVENTVALIDATION", eventValidation)
.data("ctl00$Main$Login1$UserName", username)
.data("ctl00$Main$Login1$Password", password)
.data("ctl00$Main$Login1$LoginButton", "Sign In")
.followRedirects(false)
.method(Method.POST)
.execute();
System.out.println("post cookies: " + response.cookies());
cookies.putAll(response.cookies());
System.out.println("response cookies: " + cookies);
response = Jsoup
.connect("https://apps.coned.com/cemyaccount/SessionTransfer.aspx?dir=2asp&url=https:"
+ "//apps.coned.com/csol/MainHome.asp?src=DOTNET")
.cookies(cookies)
.followRedirects(false)
.method(Method.GET)
.execute();
cookies.putAll(response.cookies());
System.out.println("response cookies: " + cookies);
String guid = response.header("location");
response = Jsoup
.connect("https://apps.coned.com/csol/SessionTransfer.asp?dir=2asp&guid="
+ guid + "&url=https://apps.coned.com/csol/MainHome.asp"
+ "?src=DOTNET&frm=")
.cookies(cookies)
.method(Method.GET)
.execute();
cookies.putAll(response.cookies());
System.out.println("response cookies: " + cookies);
Document dataPage = Jsoup
.connect("https://apps.coned.com/CEMyAccount/CSOL/BillHistory.aspx?lang=eng")
.cookies(cookies)
.get();
System.out.println("data page: " + dataPage);
Elements data = dataPage.select("table.ctl00_Main_lvBillHistory_Table1");
System.out.println("data: " + data);
在輸出中,我獲得了所有cookie,但POST cookie是空白的。
Network
選項卡選項。 Type
列。 搜索具有值document
的行。 選擇它。 這將打開一個新屏幕。 Headers
並向下滾動,直到找到“ Request Headers
。 在這里,您將找到從瀏覽器發出的請求。 您將在此處找到已發送到服務器的所有值。 以上所有步驟均適用於Chrome瀏覽器。
答案非常簡單-下划線的標頭有2個下划線,我只使用了1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.