java访问需要登录后才能访问的页面一待续

王朝学院·作者佚名  2016-08-27  
宽屏版  字体: 小 | 中 | 大 | 超大  

我们平时所访问的页面有的仅仅是一些普通的页面,有的需要用户登录后方可使用,或者需要认证以及是一些通过加密方式传输,例如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:publicclassBlogrunning {

23:

24:privateList<String> cookies;

25:privateHttpURLConnection conn;

26:

27:privatefinal String USER_AGENT ="Mozilla/5.0";

28:

29:/**

30:* @param args

31:* @throws Exception

32:*/

33:publicstaticvoidmain(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 =newBlogrunning();

38:

39:CookieHandler.setDefault(newCookieManager());

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:privatevoidsendPost(String url, String postParams) throws IOException {

52:URL obj =newURL(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+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.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 =newDataOutputStream(conn.getOutputStream());

75:wr.writeBytes(postParams);

76:wr.flush();

77:wr.close();

78:

79:intresponseCode = 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:BufferedReaderin=

85:newBufferedReader(newInputStreamReader(conn.getInputStream()));

86:String inputLine;

87:StringBuffer response =newStringBuffer();

88:

89:while((inputLine =in.readLine()) !=null) {

90:response.append(inputLine);

91:}

92:in.close();

93:}

94:privateString 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 =newArrayList<String>();

102:for(Element inputElement : inputElements) {

103:String key = inputElement.attr("id");

104:Stringvalue= inputElement.attr("value");

105:if(key.equals("input1"))

106:value= username;

107:elseif(key.equals("input2"))

108:value= password;

109:elseif(key.equals("remember_me"));

110:value="true";

111:paramList.add(key +"="+ URLEncoder.encode(value,"UTF-8"));

112:}

113:

114:StringBuilder result =newStringBuilder();

115:for(String param : paramList) {

116:if(result.length() == 0) {

117:result.append(param);

118:}else{

119:result.append("&"+ param);

120:}

121:}

122:returnresult.toString();

123:}

124:

125:publicList<String> getCookies() {

126:returncookies;

127:}

128:

129:publicvoidsetCookies(List<String> cookies) {

130:this.cookies = cookies;

131:}

132:privateString GetPageContent(String url) throws Exception {

133:URL obj =newURL(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:intresponseCode = conn.getResponseCode();

152:System.out.println("\nSending 'GET' request to URL : "+ url);

153:System.out.println("Response Code : "+ responseCode);

154:

155:BufferedReaderin=

156:newBufferedReader(newInputStreamReader(conn.getInputStream()));

157:String inputLine;

158:StringBuffer response =newStringBuffer();

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:returnresponse.toString();

169:}

170:

171:}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有