我已經詳細閱讀了關於如何做到這一點以及我嘗試過多種不同的變體,但是我無法使其工作。使用JSOUP登錄ConEd網站
基本上,我只想登錄到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塊餅乾設置,但然後將其發送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之前去主頁
有誰知道我怎麼可以按照所有這些重定向,並在最後得到餅乾?
這是我目前有的,但我無法從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。
嘿alkis,這些肯定是我走上找到我目前正在發送的步驟。 – ComlyW
下面是我看到的一個屏幕截圖 - http://snag.gy/3h0ir.jpg。我真的專注於表單數據,但我試圖包含所有的請求頭(Cookies除外),並且我在同一個地方結束了(打印出登錄頁面) – ComlyW
再次嘗試使用完整的用戶代理(以防萬一) 。也包括推薦人。 – alkis