bash script to login to webpage

前端 未结 4 2079
粉色の甜心
粉色の甜心 2021-01-20 11:06

I am trying to login into this page but I cannot for the life of me get it to work. I have to login to this site when i connect to my school\'s wifi in order to start a sess

4条回答
  •  天涯浪人
    2021-01-20 11:28

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.util.List;
    import java.util.Map.Entry;
    import java.util.Vector;
    
    
    public class AuthUrl {
        String username="";
        String password="";
        private Boolean error=false;
        private static final String conurl="campus.fsu.edu";
        private static final String  auth_url="http://campus.fsu.edu/webapps/loggedin.htm";
    
    
        public AuthUrl() {}
    
    
        public AuthUrl( String username, String password) { 
            this.username = username;
            this.password = password;   
        }
    
    
    
    
        public String Result()  {
    
    
        String url = "http://"+conurl+"/webapps/login/bb_bb60/logincas.jsp";
        String charset = "UTF-8";
    
    
        StringBuilder sba=new StringBuilder();
        sba.append("Username: "+username+"  ("+conurl+")
    "); try { String query = String.format("qualifier=%s&username=%s&password=%s", URLEncoder.encode(username, charset), URLEncoder.encode(password, charset)); HttpURLConnection authi = (HttpURLConnection) new URL(url + "?" + query).openConnection(); authi.connect(); StringBuilder sb = new StringBuilder(); // find the cookies in the response header from the first request List cookies = authi.getHeaderFields().get("Set-Cookie"); if (cookies != null) { for (String cookie : cookies) { if (sb.length() > 0) { sb.append("; "); } // only want the first part of the cookie header that has the value String value = cookie.split(";")[0]; sb.append(value); } } //If authentication passed the Location field will have a value //however the user has put in invalid details the Location within header will be null List location = authi.getHeaderFields().get("Location"); if (location == null) { error=true; } if (error==false) { // build request cookie header to send on all subsequent requests String cookieHeader = sb.toString(); // with the cookie header your session should be preserved // Now connect to authenticated url - and show its contents URL regUrl = new URL(auth_url); HttpURLConnection regCon = (HttpURLConnection) regUrl.openConnection(); regCon.setRequestProperty("Cookie", cookieHeader); regCon.connect(); //int rc = regCon.getResponseCode(); //for (Entry> header : regCon.getHeaderFields().entrySet()) { // System.out.println(header.getKey() + "=" + header.getValue()); //} BufferedReader br = new BufferedReader(new java.io.InputStreamReader(regCon.getInputStream())); String line = null; String searchingfor=""; while ((line = br.readLine()) != null){ // To Parse the results over here logics would be needed such as //if (line.indexOf("some_string")>-1) { // searhingfor=line.substring(line.indexOf("Pattern before),line.indexOf("endstring")+4) //where 4 if number of characters end string was. //} //above is commented for now print each line sba.append(line+"
    "); } } }else{ sba.append("Authentication has failed - credintials did not meet the criteria for username:"+ username+" on url: "+url+ "?" + query); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return(sba.toString()); } }

    This is the same answer written in java and works with http https

提交回复
热议问题