2013-10-16 204 views
1

我想爲我的學校wifi登錄系統做一個自動登錄程序。我需要對認證URL(https://ccahack.bergen.org/auth/perfigo_validate.jsp)執行POST請求並提交一些參數。如果我將HTTPS更改爲HTTP並執行POST請求,它只是將我重定向到登錄論壇。因此,當務之急是將帖子發到了HTTPS,但問題是,我得到一個SSL套接字錯誤,這裏是我的代碼:爲什麼我的POST請求(HTTPS)請求失敗?

public boolean signIn() throws Exception{ 
     //System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36"); 
     String httpsURL = "https://ccahack.bergen.org/auth/perfigo_validate.jsp"; 
     String username = "username"; 
     String password = "password"; 
     StringBuilder q = new StringBuilder(); 
     q.append("reqFrom="+URLEncoder.encode("perfigo_simple_login.jsp","UTF-8")); 
     q.append("&uri="+ URLEncoder.encode("https://ccahack.bergen.org/","UTF-8")); 
     q.append("&cm=" + URLEncoder.encode("ws32vklm", "UTF-8")); 
     q.append("&userip="+URLEncoder.encode("IP_ADDRESS HERE","UTF-8")); 
     q.append("&os=" +URLEncoder.encode("MAC_OSX","UTF-8")); 
     q.append("&index="+URLEncoder.encode("4","UTF-8")); 
     q.append("&username="+URLEncoder.encode(username,"UTF-8")); 
     q.append("&password="+URLEncoder.encode(password,"UTF-8")); 
     q.append("&provider="+URLEncoder.encode("BCA","UTF-8")); 
     q.append("&login_submt="+URLEncoder.encode("Continue","UTF8")); 
     String query = q.toString(); 
     URL myurl = new URL(httpsURL); 
     HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); 
     con.setRequestMethod("POST"); 
     con.setRequestProperty("Content-length", String.valueOf(query.length())); 
     con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
     con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)"); 
     con.setDoOutput(true); 
     con.setDoInput(true); 

     DataOutputStream output = new DataOutputStream(con.getOutputStream()); 
      //error here 

     output.writeBytes(query); 

     output.close(); 

     DataInputStream input = new DataInputStream(con.getInputStream()); 



     for(int c = input.read(); c != -1; c = input.read()) 
     System.out.print((char)c); 
     input.close(); 

     System.out.println("Resp Code:"+con.getResponseCode()); 
     System.out.println("Resp Message:"+ con.getResponseMessage()); 
     return false; 
    } 
+3

如果您發佈錯誤,它將有所幫助。 –

+0

javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路徑構建失敗:sun.security.provider.certpath.SunCertPathBuilderException:無法找到要求的目標的有效證書路徑 – parasm

回答

1

根據您的評論張貼例外,您將需要確保該網站的證書位於您的信任庫中。 (這裏假設你信任它)

Here'e東西,我寫了一段時間回來:

http://springinpractice.com/2012/04/29/fixing-pkix-path-building-issues-when-using-javamail-and-smtp/

在這種情況下,我在做的SMTP(如果你正在做的HTTP),所以你會需要爲此做出調整。

+0

我對要做什麼感到困惑。我知道我需要下載證書,但是我使用了什麼命令?像這樣?: openssl s_client -connect mail.kattare.com:2525https://ccahack.bergen.org/auth/perfigo_validate.jsp:80 -htpp http bergen.cer – parasm