国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

java訪問需要登錄后才能訪問的頁面一待續(xù)

2019-11-14 15:33:06
字體:
供稿:網(wǎng)友
我們平時所訪問的頁面有的僅僅是一些普通的頁面,有的需要用戶登錄后方可使用,或者需要認證以及是一些通過加密方式傳輸,例如HTTPS。很多頁面需要我們注冊登錄后才能訪問,這個時候就涉及到cookie的處理問題,為了使我們的程序能使用別人所提供的服務(wù)頁面,就要求程序首先登錄后再訪問服務(wù)頁面,這過程就需 要自行處理cookie。原文地址http://www.yihaomen.com/article/java/371.htm
大概分為3個步驟
第一步 發(fā)送一個Get請求 String page = http.GetPageContent(url);  其實就是為了獲取到一個訪問的Document將流轉(zhuǎn)化為字符串,方便訪問。
第二步 在發(fā)送post請求時需要發(fā)送對應(yīng)的用戶名密碼,這個也是關(guān)鍵所在,因為一般不是你開發(fā)的網(wǎng)站你不清楚要傳遞的參數(shù)名是什么,通過Jsoup解析網(wǎng)頁,循環(huán)遍歷其中的input控件,將需要的用戶名密碼拼接起來,類似郵箱就是u=username&p=passWord
第三步  發(fā)送post請求,將拼接的字符串作為流讀進去
第四步  讀取cookie,帶著cookie一起訪問下一個需要登錄后訪問的頁面
最后  未成功訪問,估計錯誤在第二步,路過的請?zhí)釋氋F意見,直接上代碼
 

1: package BlogTest;

   2:   
   3:  import java.io.BufferedReader;
   4:  import java.io.DataOutputStream;
   5:  import java.io.IOException;
   6:  import java.io.InputStreamReader;
   7:  import java.io.UnsupportedEncodingException;
   8:  import java.net.CookieHandler;
   9:  import java.net.CookieManager;
  10:  import java.net.URL;
  11:  import java.net.URLEncoder;
  12:  import java.util.ArrayList;
  13:  import java.util.List;
  14:   
  15:  import org.jsoup.Jsoup;
  16:  import org.jsoup.nodes.Document;
  17:  import org.jsoup.nodes.Element;
  18:  import org.jsoup.select.Elements;
  19:   
  20:  import sun.net.www.PRotocol.http.HttpURLConnection;
  21:   
  22:  public class Blogrunning {
  23:   
  24:      private List<String> cookies;
  25:      private HttpURLConnection conn;
  26:   
  27:      private final String USER_AGENT = "Mozilla/5.0";
  28:      
  29:      /**
  30:       * @param args
  31:       * @throws Exception 
  32:       */
  33:      public static void main(String[] args) throws Exception {
  34:          String url = "http://passport.VEVb.com/user/signin";
  35:          String redirturl = "http://home.VEVb.com/u/Sir-Lin/followers/1/";
  36:   
  37:          Blogrunning http = new Blogrunning();
  38:   
  39:          CookieHandler.setDefault(new CookieManager());
  40:   
  41:          // 1. 發(fā)送get請求
  42:          String page = http.GetPageContent(url);
 
  43:         String postParams = http.getFormParams(page, "username", "password");
  44:          // 2. 發(fā)送post請求
  45:          http.sendPost(url, postParams);
  46:          
  47:          // 3. success
  48:          String result = http.GetPageContent(redirturl);
  49:          System.out.println(result);
  50:      }
 
 
 
  51:      private void sendPost(String url, String postParams) throws IOException {
  52:          URL obj = new URL(url);
  53:          conn = (HttpURLConnection) obj.openConnection();
  54:   
  55:          conn.setUseCaches(false);
  56:          conn.setRequestMethod("POST");
  57:          conn.setRequestProperty("Host", "passport.VEVb.com");
  58:          conn.setRequestProperty("User-Agent", USER_AGENT);
  59:          conn.setRequestProperty("Accept",
  60:              "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  61:          conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  62:          for (String cookie : this.cookies) {
  63:              conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
  64:          }
  65:          conn.setRequestProperty("Connection", "keep-alive");
  66:          conn.setRequestProperty("Referer", "http://passport.VEVb.com/user/signin");
  67:          conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  68:          conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
  69:   
  70:          conn.setDoOutput(true);
  71:          conn.setDoInput(true);
  72:   
  73:          //發(fā)送post請求
  74:          DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
  75:          wr.writeBytes(postParams);
  76:          wr.flush();
  77:          wr.close();
  78:   
  79:          int responseCode = conn.getResponseCode();
  80:          System.out.println("/nSending 'POST' request to URL : " + url);
  81:          System.out.println("Post parameters : " + postParams);
  82:          System.out.println("Response Code : " + responseCode);
  83:   
  84:          BufferedReader in = 
  85:                   new BufferedReader(new InputStreamReader(conn.getInputStream()));
  86:          String inputLine;
  87:          StringBuffer response = new StringBuffer();
  88:   
  89:          while ((inputLine = in.readLine()) != null) {
  90:              response.append(inputLine);
  91:          }
  92:          in.close();
  93:      }
 
 
 
 
 
  94:      private String getFormParams(String html, String username, String password) throws UnsupportedEncodingException {
  95:          System.out.println("Extracting form's data...");
  96:   
  97:          Document doc = Jsoup.parse(html);
  98:   
  99:          Element loginform = doc.getElementById("Main");
 100:          Elements inputElements = loginform.getElementsByTag("input");
 101:          List<String> paramList = new ArrayList<String>();
 102:          for (Element inputElement : inputElements) {
 103:              String key = inputElement.attr("id");
 104:              String value = inputElement.attr("value");
 105:              if (key.equals("input1"))
 106:                  value = username;
 107:              else if (key.equals("input2"))
 108:                  value = password;
 109:              else if (key.equals("remember_me"));
 110:                  value = "true";
 111:              paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
 112:          }
 113:   
 114:          StringBuilder result = new StringBuilder();
 115:          for (String param : paramList) {
 116:              if (result.length() == 0) {
 117:                  result.append(param);
 118:              } else {
 119:                  result.append("&" + param);
 120:              }
 121:          }
 122:          return result.toString();
 123:        }
 124:  
 
 
 
 125:        public List<String> getCookies() {
 126:          return cookies;
 127:        }
 128:   
 129:        public void setCookies(List<String> cookies) {
 130:          this.cookies = cookies;
 131:        }
 
 
 
 
 
 132:      private String GetPageContent(String url) throws Exception {
 133:          URL obj = new URL(url);
 134:          conn = (HttpURLConnection) obj.openConnection();
 135:   
 136:          conn.setRequestMethod("GET");
 137:   
 138:          conn.setUseCaches(false);
 139:   
 140:          conn.setRequestProperty("User-Agent", USER_AGENT);
 141:          conn.setRequestProperty("Accept",
 142:              "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
 143:          conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.5");
 144:          if (cookies != null) {
 145:              for (String cookie : this.cookies) {
 146:                  conn.addRequestProperty("Cookie", cookie.split(";",1)[0]);
 147:                  System.out.println("cookie:   "+cookie.split(";",1)[0]);
 148:              }
 149:          }
 150:          
 151:          int responseCode = conn.getResponseCode();
 152:          System.out.println("/nSending 'GET' request to URL : " + url);
 153:          System.out.println("Response Code : " + responseCode);
 154:   
 155:          BufferedReader in = 
 156:                  new BufferedReader(new InputStreamReader(conn.getInputStream()));
 157:          String inputLine;
 158:          StringBuffer response = new StringBuffer();
 159:   
 160:          while ((inputLine = in.readLine()) != null) {
 161:              response.append(inputLine);
 162:          }
 163:          in.close();
 164:   
 165:          // Get the response cookies
 166:          setCookies(conn.getHeaderFields().get("Set-Cookie"));
 167:   
 168:          return response.toString();
 169:      }
 170:   
 171:  }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广昌县| 梓潼县| 彭泽县| 新野县| 布尔津县| 永吉县| 临漳县| 广东省| 浪卡子县| 故城县| 高陵县| 云浮市| 萨嘎县| 务川| 宜阳县| 宜良县| 天柱县| 来宾市| 揭东县| 盐亭县| 新绛县| 贺兰县| 宁远县| 肥东县| 元阳县| 东丰县| 华安县| 浦县| 灌阳县| 宣恩县| 米易县| 广南县| 南汇区| 泸西县| 巨野县| 虹口区| 罗甸县| 新乐市| 黄山市| 金溪县| 垦利县|