你的位置:首页 > Java教程

[Java教程]java访问需要登录后才能访问的页面一待续


我们平时所访问的页面有的仅仅是一些普通的页面,有的需要用户登录后方可使用,或者需要认证以及是一些通过加密方式传输,例如HTTPS。很多页面需要我们注册登录后才能访问,这个时候就涉及到cookie的处理问题,为了使我们的程序能使用别人所提供的服务页面,就要求程序首先登录后再访问服务页面,这过程就需 要自行处理cookie。原文地址http://www.yihaomen.com/article/java/371.htm
大概分为3个步骤
第一步 发送一个Get请求 String page = http.GetPageContent(url);  其实就是为了获取到一个访问的Document将流转化为字符串,方便访问。
第二步 在发送post请求时需要发送对应的用户名密码,这个也是关键所在,因为一般不是你开发的网站你不清楚要传递的参数名是什么,通过Jsoup解析网页,循环遍历其中的input控件,将需要的用户名密码拼接起来,类似邮箱就是u=username&p=password

第三步  发送post请求,将拼接的字符串作为流读进去
第四步  读取cookie,带着cookie一起访问下一个需要登录后访问的页面
最后  未成功访问,估计错误在第二步,路过的请提宝贵意见,直接上代码
 

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.cnblogs.com/user/signin";

 35:     String redirturl = "http://home.cnblogs.com/u/Sir-Lin/followers/1/";

 36:  

 37:     Blogrunning http = new Blogrunning();

 38:  

 39:     CookieHandler.setDefault(new CookieManager());

 40:  

 41:     // 1. 发送get请求

 42:     String page = http.GetPageContent(url);

 

 43:     String postParams = http.getFormParams(page, "username", "password");

 44:     // 2. 发送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.cnblogs.com");

 58:     conn.setRequestProperty("User-Agent", USER_AGENT);

 59:     conn.setRequestProperty("Accept",

 60:       "text/html,application/xhtml+);

 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.cnblogs.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:     //发送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+);

 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: }