Java Method to Enter ASP.NET Web Form

I am working on a java program that will have to go into an ASP.NET web form and then upload the file after authentication. Regular HTTP GET / POST is not a problem, but it seems that ASP does not give me the SESSION identifier when connecting with java, but it is from the browser.

When I look at the header information in Firefox, I see that the cookies are set from the initial login, but then the page is immediately redirected to the new URL. I'm not sure if this matters, but the page to which it is redirected after logging in contains frames. I tried loading both the main page and the iframe src inside, but did not give me a cookie in the header.

//Pull up the login page, extract out the hidden input variables __VIEWSTATE, __EVENTVALIDATION
URL url = new URL(loginPage);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
//This reads the page line-by-line and extracts out all the values from hidden input fields
Map<String,String> formFields = getViewstate(conn);

//Now re-open the URL to actually submit the POST data
conn = (HttpURLConnection) url.openConnection();            
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String postValues = URLEncoder.encode("txtUsername", "UTF-8") + "=" + URLEncoder.encode(uid, "UTF-8");
postValues += "&" + URLEncoder.encode("txtPassword", "UTF-8") + "=" + URLEncoder.encode(pwd, "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTTARGET", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
postValues += "&" + URLEncoder.encode("__VIEWSTATE", "UTF-8") + "=" + URLEncoder.encode(formFields.get("viewstate"), "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTVALIDATION", "UTF-8") + "=" + URLEncoder.encode(formFields.get("eventvalidation"), "UTF-8");
out.writeBytes(postValues);
out.flush();
out.close();
//At this point looking at Firefox sniffer data, it should be sending back the cookie
//However there is no Set-Cookie in the header fields
for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
        // get ASP.NET_SessionId from cookie
    if (key.equalsIgnoreCase("set-cookie")) {
        sessionId = conn.getHeaderField(key);
        sessionId = sessionId.substring(0, sessionId.indexOf(";"));
    }
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
    //The page it prints out is the page it was redirected to when logged in through the browser
    System.out.println(line);
}
rd.close();
//At this point, it was a successful login, but I never got the cookie so I'm stuck
+3
2

HttpClient, , HtmlUnit , , , . cookie, , , , - . , - Selenium/Webdriver, .

+2

, , , Cookies, HttpURLConnection. , HtmlUnit, ( cookie, javascript ..).

0

Source: https://habr.com/ru/post/1780706/


All Articles