2012-09-03 88 views
3

我想登录到this page但我不能为我的生活得到它的工作。当我连接到我学校的无线网络以开始会话时,我必须登录到此网站。bash脚本登录到网页

到目前为止,我试图使用bash和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/ 
+1

您可能遇到了Cookie或其他问题。因此使用正确的设置卷曲可能是可能的。尝试tcpdumping时,你手工做,然后tcpdump卷曲会议,寻找差异 –

+0

你可以发布你如何尝试?你可以用curl登录到你的学校页面,但它会是不同的浏览器。所以,即使您以这种方式登录,您也没有浏览器级别的会话。 – Cyprian

+0

这样做的主要原因是自动登录到wifi的过程。会议是否会开始这种事情? – Ntc

回答

3

有些东西你明明做错了 - 你的形式如下:

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

你需要将您的请求提交给表单的“操作”网址,并且您需要将其作为POST请求而不是对其他某个URL的GET请求。

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进行身份验证

所以最初进行身份验证,然后 - 再次调用卷曲和负载cookie来验证URL

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: