本文實例為大家分享了Java模擬HTTP Get Post請求,校園BBS自動回帖功能,供大家參考,具體內(nèi)容如下
設(shè)計思路
找到帖子鏈接的集合,最后面數(shù)字變化, 就可以得到不同的帖子
防止帖子發(fā)表會又被刪了的情況, 進(jìn)行判斷帖子是否存在
遍歷這個集合, 對每個鏈接做回帖的POST請求
重難點
Note:
- 回帖需要用戶登錄信息
- 一種是利用Cookie
- 另一種是進(jìn)行模擬登錄
- 本文采用前者
代碼
代碼比較簡單,注意事項是找到自己的Cookie,賦給String yourCookeie就可以直接運行
主要就是判斷帖子存不存在,這是一個get請求,然后用post發(fā)送一個回帖,回帖信息在mapData.put(“message”, “友情幫頂了”)中 硬編碼為”友情幫頂了”,你可以修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.LinkedHashMap; import java.util.Map; public class Inter { private static final String baseRefer = "http://rs.xidian.edu.cn/forum.php?mod=viewthread&tid=" ; private static final String yourCookeie = "Q8qA_2132_saltkey=g1NJjJ3O; Q8qA_2132_lastvisit=1438243699; Q8qA_2132_lastcheckfeed=256730%7C1438252008; Q8qA_2132_auth=e11aEhhXpLgTYpfDK72YJZEgJHL1v70cUXXDtJ71VbU2dyuH%2BQHw3pGOJhsFxfjbVgNsvyfG1v%2BQlD0lt8kg6J%2B40W0; Q8qA_2132_st_t=256730%7C1438571068%7C51f8a322985e44f65ff1143329e6779a; Q8qA_2132_forum_lastvisit=D_106_1438571068; Q8qA_2132_myrepeat_rr=R0; Q8qA_2132_ulastactivity=d7degfMAwG5AGHshmT%2BwCq1L91znQpEa57p%2F0Vt7VHdC8DrOuGTT; Q8qA_2132_home_diymode=1; tjpctrl=1438781938176; Q8qA_2132_visitedfid=72D551D215D110D13D142D22D91D217D548; Q8qA_2132_st_p=256730%7C1438781224%7C7a73ef608dc3caf733308d63639b3bd0; Q8qA_2132_viewid=tid_773850; Q8qA_2132_smile=10D1; Q8qA_2132_sid=ZnfqQN; Q8qA_2132_lastact=1438781403%09forum.php%09ajax" ; public static void main(String[] args) { int startId = 774210 ; // you need change for ( int i = 0 ; i < 100 ; i++) { postMessage(startId); startId++; } } public static boolean isExist( int id) { String tmpPath = baseRefer + id; URL url; try { url = new URL(tmpPath); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.addRequestProperty( "Content-Type" , "text/html; charset=UTF-8" ); con.addRequestProperty( "User-Agent" , "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36" ); con.addRequestProperty( "Referer" , "http://t.dianping.com/register" ); con.setRequestMethod( "GET" ); if (con.getResponseCode() == 200 ) { InputStream inputStr = con.getInputStream(); String info = new String(StreamTool.read(inputStr), "UTF-8" ); if (info.contains( "抱歉,指定的主題不存在或已被刪除或正在被審核" )) { System.out.println( "id=" + id + "帖子存在或已被刪除!" ); return false ; } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return true ; } public static void postMessage( int id) { if (!isExist(id)) { return ; } String tmpPath = baseRefer + id; StringBuilder path = new StringBuilder(tmpPath); Map<String, String> mapData = new LinkedHashMap<String, String>(); mapData.put( "mod" , "post" ); mapData.put( "action" , "reply" ); mapData.put( "replysubmit" , "yes" ); mapData.put( "infloat" , "yes" ); mapData.put( "handlekey" , "fastpost" ); mapData.put( "inajax" , "1" ); mapData.put( "message" , "友情幫頂了" ); mapData.put( "formhash" , "86ec5d81" ); try { for (Map.Entry<String, String> mapEnt : mapData.entrySet()) { path.append( "&" ); path.append(mapEnt.getKey() + "=" ); path.append(URLEncoder.encode(mapEnt.getValue(), "UTF-8" )); } URL url = new URL(path.toString()); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod( "POST" ); con.setRequestProperty( "Content-Type" , "application/x-www-form-urlencoded" ); con.setRequestProperty( "Content-Length" , String.valueOf(path.length())); con.setRequestProperty( "User-Agent" , "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36" ); con.setRequestProperty( "Cookie" , yourCookeie); con.setDoOutput( true ); OutputStream outStr = con.getOutputStream(); outStr.write(path.toString().getBytes()); if (con.getResponseCode() == 200 ) { InputStream inputStr = con.getInputStream(); String info = new String(StreamTool.read(inputStr), "UTF-8" ); System.out.println( "在id=" + id + "成功發(fā)帖!" ); try { Thread.sleep( 20 * 1000 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class StreamTool { public static byte [] read(InputStream inputStr) throws Exception { ByteArrayOutputStream outStr = new ByteArrayOutputStream(); // TODO Auto-generated method stub byte [] buffer = new byte [ 1024 ]; int len = 0 ; while ((len = inputStr.read(buffer)) != - 1 ) { outStr.write(buffer, 0 , len); } inputStr.close(); return outStr.toByteArray(); } } |
效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,實現(xiàn)帖子自動回復(fù)。