java.net類比使用者登入網頁並維持session的方法

來源:互聯網
上載者:User

有的時候,會需要使用java的程式訪問網頁,正常的訪問網頁的程式很多,也沒有什麼可講的,在這裡主要說明一下如何使用java的訪問網頁時,需要登入時,如何保持登入的程式。

先簡單講解一下原理:實際上網頁和伺服器是兩套應用,Client Access Server一次,實際上網頁擷取後,串連立即就斷了,這樣的話,說明伺服器是不會保持長效串連的,但是在現實情況登入的話,用戶端好像一直在保留串連,那麼這種情況就和先前模式似乎不同;當然不是,中斷連線是整個B/S架構的根本原理所在,因此怎麼去保持這些串連,實際上就是利用Cookie,訪問網頁時,每次網頁的伺服器會產生一個Cookie,記錄當前網頁的一個索引值,如果使用登入了後,伺服器端的Session就會和用戶端的一個Cookie產生串連,這樣每次給這個頁面發送用戶端的Cookie所記錄的Session的標識,即可完成登入的類比。
代碼如下:

[java]
view plaincopyprint?
  1. URL url = new URL("網頁"); 
  2. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
  3. connection.setDoOutput(true);//允許串連提交資訊 
  4. connection.setRequestMethod("POST");//網頁提交方式“GET”、“POST” 
  5. connection.setRequestProperty("User-Agent",
    "Mozilla/4.7 [en] (Win98; I)"); 
  6. StringBuffer sb = new StringBuffer(); 
  7. sb.append("username=admin"); 
  8. sb.append("&password=admin"); 
  9. OutputStream os = connection.getOutputStream(); 
  10. os.write(sb.toString().getBytes()); 
  11. os.close(); 
  12.  
  13.  
  14. BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
  15.  
  16.  
  17.  
  18.  
  19. String responseCookie = connection.getHeaderField("Set-Cookie");//取到所用的Cookie 
  20.            System.out.println("cookie:" + responseCookie); 
  21. String line = br.readLine(); 
  22.  
  23.  
  24. while (line != null) { 
  25.  
  26.  
  27. System.out.println(new String(line.getBytes())); 
  28.  
  29.  
  30. line = br.readLine();//打出登入的網頁 
  31.  
  32.  
  33. //acces 
  34. URL url1 = new URL("網頁的登入後的頁面"); 
  35. HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection(); 
  36. connection1.setRequestProperty("Cookie", responseCookie);//給伺服器送登入後的cookie 
  37. BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream())); 
  38.  
  39.  
  40. String line1= br1.readLine(); 
  41.  
  42.  
  43. while (line1 != null) { 
  44.  
  45.  
  46. System.out.println(new String(line1.getBytes())); 
  47.  
  48.  
  49. line1 = br1.readLine(); 
  50.  
  51.  

URL url = new URL("網頁");<br />HttpURLConnection connection = (HttpURLConnection) url.openConnection();<br />connection.setDoOutput(true);//允許串連提交資訊<br />connection.setRequestMethod("POST");//網頁提交方式“GET”、“POST”<br />connection.setRequestProperty("User-Agent", "Mozilla/4.7 [en] (Win98; I)");<br />StringBuffer sb = new StringBuffer();<br />sb.append("username=admin");<br />sb.append("&password=admin");<br />OutputStream os = connection.getOutputStream();<br />os.write(sb.toString().getBytes());<br />os.close();</p><p>BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));</p><p>String responseCookie = connection.getHeaderField("Set-Cookie");//取到所用的Cookie<br /> System.out.println("cookie:" + responseCookie);<br />String line = br.readLine();</p><p>while (line != null) {</p><p>System.out.println(new String(line.getBytes()));</p><p>line = br.readLine();//打出登入的網頁</p><p>}<br />//acces<br />URL url1 = new URL("網頁的登入後的頁面");<br />HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();<br />connection1.setRequestProperty("Cookie", responseCookie);//給伺服器送登入後的cookie<br />BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream()));</p><p>String line1= br1.readLine();</p><p>while (line1 != null) {</p><p>System.out.println(new String(line1.getBytes()));</p><p>line1 = br1.readLine();</p><p>}</p><p>
不過現在很多網站都是用了網頁的Token機制,也就是說每個頁面都會產生一個唯一索引值,而且再加上登入的驗證碼的過程,這樣很多網頁的程式就不能再單純的使用這個網頁登入了,但是這個機制可以讓大家明白怎麼去保持用戶端Session的串連過程。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.