3

我正在尝试登录此页面,但我终其一生都无法使其正常工作。当我连接到学校的 wifi 时,我必须登录该站点才能开始会话。

到目前为止,我尝试使用 bash 和 cUrl 来实现这一点,但只是让自己头疼。cUrl 会起作用还是我走错了路?任何帮助是极大的赞赏!

谢谢,

ñ

这是我尝试过的:

curl --cookie-jar cjar --output /dev/null http://campus.fsu.edu/webapps/login/

curl --cookie cjar --cookie-jar cjar \
    --data 'username=foo' \
    --data 'password=bar' \
    --data 'service=http://campus.fsu.edu/webapps/login/' \
    --data 'loginurl=http://campus.fsu.edu/webapps/login/bb_bb60/logincas.jsp' \
    --location \
    --output ~/loginresult.html \
        http://campus.fsu.edu/webapps/login/
4

4 回答 4

3

有些事情你显然做错了——你的表格看起来像这样:

<form onsubmit="return validate_form(this)" method="post" action="https://bb5.fsu.edu/cas/" id="login" AUTOCOMPLETE="off">
<!-- etc -->  
</form>

您需要将请求提交到表单的“操作”URL,并且需要将其设为 POST 请求,而不是对其他 URL 的 GET 请求。

于 2012-09-04T12:31:10.540 回答
2
curl -c vh.cookie "http://campus.fsu.edu/webapps/login/bb_bb60/logincas.jsp?username=foor&password=bar"

 curl -b vh.cookie "http://campus.fsu.edu/webapps/login/login.result"

其中http://campus.fsu.edu/webapps/login/login.result是用户通过身份验证后的结束 url

所以最初进行身份验证 - 再次调用 curl 并将 cookie 加载到经过身份验证的 url

于 2012-09-04T13:01:15.777 回答
1

我设法用python和mechanize做到了这一点。也应该为你工作:

http://stockrt.github.com/p/handling-html-forms-with-python-mechanize-and-BeautifulSoup/

您还可以下载 selenium 插件并将您的登录场景导出为 python、ruby 或 java 脚本。下载一些库后,您将获得相同的效果。

于 2012-09-04T12:41:18.533 回答
1
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+")<br>");
    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<String> 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<String> 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<String, List<String>> 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+"<br>");
        }

        }

        }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());
}
}

这是用 java 编写的相同答案,适用于 http https

于 2012-09-04T13:08:27.707 回答