java HttpURLConnection來實作get及post動作
這個範例可以利用java.net.HttpURLconnection來摸擬瀏覽網頁
做form submit動作
public boolean doPost(String sURL,String data,String cookie,String referer,String charset)
post部份需要傳入
sURL:Action的url
data :要傳送的的資料也就是像id=123&test=456之類的
cookie:是否要傳送cookie資料,可為null,像 __utma=114386561.1334910113.1250671126.1251247266.1251279995.24;
referer:傳那裡來的,是一個網址,可為null
charset:傳送及取回的資料編碼為何
public boolean doGet(String sURL,String cookie,String referer,String charset)
跟post唯一不同的地方為
sURL:Action的url 再加上?data ,像http://www.aaa.com/123.jsp?id=123&test=456
ps.範例使用log4j,如果不會用的人可以直接把log4j部份改成System.out.println
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
|
private org.apache.log4j.Logger logger;
public WebModule() {
logger = org.apache.log4j.Logger.getLogger(this.getClass());
}
public boolean doPost(String sURL, String data, String cookie,
String referer, String charset) {
boolean doSuccess = false;
java.io.BufferedWriter wr = null;
try {
URL url = new URL(sURL);
HttpURLConnection URLConn = (HttpURLConnection) url
.openConnection();
URLConn.setDoOutput(true);
URLConn.setDoInput(true);
((HttpURLConnection) URLConn).setRequestMethod("POST");
URLConn.setUseCaches(false);
URLConn.setAllowUserInteraction(true);
HttpURLConnection.setFollowRedirects(true);
URLConn.setInstanceFollowRedirects(true);
URLConn
.setRequestProperty(
"User-agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-TW; rv:1.9.1.2) "
+ "Gecko/20090729 Firefox/3.5.2 GTB5 (.NET CLR 3.5.30729)");
URLConn
.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
URLConn.setRequestProperty("Accept-Language",
"zh-tw,en-us;q=0.7,en;q=0.3");
URLConn.setRequestProperty("Accept-Charse",
"Big5,utf-8;q=0.7,*;q=0.7");
if (cookie != null)
URLConn.setRequestProperty("Cookie", cookie);
if (referer != null)
URLConn.setRequestProperty("Referer", referer);
URLConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
URLConn.setRequestProperty("Content-Length", String.valueOf(data
.getBytes().length));
java.io.DataOutputStream dos = new java.io.DataOutputStream(URLConn
.getOutputStream());
dos.writeBytes(data);
java.io.BufferedReader rd = new java.io.BufferedReader(
new java.io.InputStreamReader(URLConn.getInputStream(),
charset));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
} catch (java.io.IOException e) {
doSuccess = false;
logger.info(e);
} finally {
if (wr != null) {
try {
wr.close();
} catch (java.io.IOException ex) {
logger.info(ex);
}
wr = null;
}
}
return doSuccess;
}
public boolean doGet(String sURL, String cookie, String referer,
String charset) {
boolean doSuccess = false;
BufferedReader in = null;
try {
URL url = new URL(sURL);
HttpURLConnection URLConn = (HttpURLConnection) url
.openConnection();
URLConn
.setRequestProperty(
"User-agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-TW; rv:1.9.1.2) "
+ "Gecko/20090729 Firefox/3.5.2 GTB5 (.NET CLR 3.5.30729)");
URLConn
.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
URLConn.setRequestProperty("Accept-Language",
"zh-tw,en-us;q=0.7,en;q=0.3");
URLConn.setRequestProperty("Accept-Charse",
"Big5,utf-8;q=0.7,*;q=0.7");
if (cookie != null)
URLConn.setRequestProperty("Cookie", cookie);
if (referer != null)
URLConn.setRequestProperty("Referer", referer);
URLConn.setDoInput(true);
URLConn.setDoOutput(true);
URLConn.connect();
URLConn.getOutputStream().flush();
in = new BufferedReader(new InputStreamReader(URLConn
.getInputStream(), charset));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
doSuccess = false;
log.out.println(e);
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (java.io.IOException ex) {
logger.info(ex);
}
in = null;
}
}
return doSuccess;
}
|
沒有留言:
張貼留言