Wednesday, April 22, 2009

java httpurlconnection 登录网站 完整代码

更多精彩请到 http://www.139ya.com



1. import java.io.*;
2. import java.util.*;
3. import java.net.*;
4.
5. public class WebTest {
6.
7. public static void main(String[] args) {
8.
9. System.out.println("beging...");
10. DownLoadPages("http://login.xiaonei.com/Login.do", "d:/fileDown.txt");
11. // visit("http://www.xiaonei.com");
12. System.out.println("end.");
13. }
14.
15. public static void DownLoadPages(String urlStr, String outPath) {
16. int chByte = 0;
17.
18. URL url = null;
19.
20. HttpURLConnection httpConn = null;
21.
22. InputStream in = null;
23.
24. FileOutputStream out = null;
25.
26. try {
27. String post = "email=" + URLEncoder.encode("e-mail", "UTF-8")
28. + "&password=" + "password";
29. url = new URL(urlStr);
30.
31. httpConn = (HttpURLConnection) url.openConnection();
32.
33. //setInstanceFollowRedirects can then be used to set if
34. //redirects should be followed or not and this should be used before the
35. //connection is established (via getInputStream, getResponseCode, and other
36. //methods that result in the connection being established).
37.
38. httpConn.setFollowRedirects(false);
39.
40. //inorder to disable the redirects
41. httpConn.setInstanceFollowRedirects(false);
42.
43. httpConn.setDoOutput(true);
44. httpConn.setDoInput(true);
45. httpConn.setRequestProperty("User-Agent",
46. "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
47. httpConn.setRequestProperty("Content-Type",
48. "application/x-www-form-urlencoded");
49.
50. //ok now, we can post it
51. PrintStream send = new PrintStream(httpConn.getOutputStream());
52. send.print(post);
53. send.close();
54. URL newURL = new URL(httpConn.getHeaderField("Location"));
55. System.out.println("the URL has move to "
56. + httpConn.getHeaderField("Location"));
57. httpConn.disconnect();
58.
59. // OK, now we are ready to get the cookies out of the URLConnection
60. String cookies = getCookies(httpConn);
61. System.out.println(cookies);
62. httpConn = (HttpURLConnection) newURL.openConnection();
63. httpConn.setRequestProperty("User-Agent",
64. "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
65. httpConn.setRequestProperty("Content-Type",
66. "application/x-www-form-urlencoded");
67. httpConn.setRequestProperty("Cookie", cookies);
68.
69. httpConn.setDoInput(true);
70. in = httpConn.getInputStream();
71. out = new FileOutputStream(new File(outPath));
72.
73. chByte = in.read();
74. while (chByte != -1) {
75. out.write(chByte);
76. //System.out.println(chByte);
77. chByte = in.read();
78. }
79. } catch (Exception e) {
80. e.printStackTrace();
81. }
82. }
83.
84. public static String getCookies(HttpURLConnection conn) {
85. StringBuffer cookies = new StringBuffer();
86. String headName;
87. for (int i = 7; (headName = conn.getHeaderField(i)) != null; i++) {
88. StringTokenizer st = new StringTokenizer(headName, "; ");
89. while (st.hasMoreTokens()) {
90. cookies.append(st.nextToken() + "; ");
91. }
92. }
93. return cookies.toString();
94. }
95. }

No comments: