-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestBuilder.java
More file actions
230 lines (191 loc) · 4.99 KB
/
RequestBuilder.java
File metadata and controls
230 lines (191 loc) · 4.99 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
/*
* Class made by Jack Chappell
* Version: 2.0
*/
/*
* Example:
*
* RequestBuilder request = new RequestBuilder()
* .setURL("http://darkcop.co.uk/BungeeStats/test.php")
* .addUrlParameter("name", "Bob")
* .addUrlParameter("mood", "Happy")
* .setRequestType(RequestType.GET)
* .setAsync(true)
* .setFinishRunnable(new RequestRunnable()
* {
* public void run(RequestInfo info)
* {
* System.out.println("Response: " + info.getResponse());
* }
* })
* .doRequest();
*
* Output:
* Response: Hello there Bob you are Happy
*/
public class RequestBuilder
{
public enum RequestType
{
POST, GET;
}
private StringBuilder urlParameters = new StringBuilder(),
response = new StringBuilder();
private RequestType requestType = RequestType.GET;
private RequestRunnable finishRunnable = null;
private boolean useCaches = false,
doInput = true,
doOutput = true,
async = false;
private String contentLanguage = "en-UK",
contentType = "application/x-www-form-urlencoded",
userAgent = "Mozilla/5.0",
url = "";
private int responseCode = 0;
public RequestBuilder setURL(String url)
{
this.url = url;
return this;
}
public RequestBuilder setRequestType(RequestType requestType)
{
this.requestType = requestType;
return this;
}
public RequestBuilder setFinishRunnable(RequestRunnable finishRunnable)
{
this.finishRunnable = finishRunnable;
return this;
}
public RequestBuilder setContentLanguage(String contentLanguage)
{
this.contentLanguage = contentLanguage;
return this;
}
public RequestBuilder setContentType(String contentType)
{
this.contentType = contentType;
return this;
}
public RequestBuilder setAsync(boolean async)
{
this.async = async;
return this;
}
public RequestBuilder useCaches(boolean useCaches)
{
this.useCaches = useCaches;
return this;
}
public RequestBuilder doInput(boolean doInput)
{
this.doInput = doInput;
return this;
}
public RequestBuilder doOutput(boolean doOutput)
{
this.doOutput = doOutput;
return this;
}
public RequestBuilder addUrlParameter(String key, String value)
{
try
{
urlParameters.append("&" + key + "=" + URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) { }
return this;
}
public RequestBuilder setUserAgent(String userAgent)
{
this.userAgent = userAgent;
return this;
}
public String getResponse()
{
return this.response.toString();
}
public int getResponseCode()
{
return responseCode;
}
public RequestBuilder doRequest()
{
Thread thread = new Thread()
{
public void run()
{
HttpURLConnection con = null;
URL newUrl = null;
try
{
String urlParams = urlParameters.toString().replaceFirst("&", "");
newUrl = new URL(requestType.equals(RequestType.POST) ? url : url + "?" + urlParams);
con = (HttpURLConnection) newUrl.openConnection();
con.setRequestMethod(requestType.toString());
con.setDoInput(doInput);
con.setDoOutput(doOutput);
con.setUseCaches(useCaches);
con.setRequestProperty("Content-Type", contentType);
con.setRequestProperty("Content-Language", contentLanguage);
con.setRequestProperty("Content-Length", urlParams.getBytes().length + "");
con.setRequestProperty("User-Agent", userAgent);
if(requestType.equals(RequestType.POST))
{
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParams);
wr.flush();
wr.close();
}
responseCode = con.getResponseCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while((line = rd.readLine()) != null)
response.append(line).append('\n');
if(response.length() != 0)
response.replace(response.length() - 1, response.length(), "");
rd.close();
}
catch(Exception e) { }
finally
{
if(con != null) con.disconnect();
if(finishRunnable != null) finishRunnable.run(new RequestInfo(response.toString(), responseCode));
}
}
};
if(async)
thread.start();
else
thread.run();
return this;
}
}
interface RequestRunnable
{
public void run(RequestInfo info);
}
class RequestInfo
{
private int responseCode;
private String response;
public RequestInfo(String response, int responseCode)
{
this.response = response;
this.responseCode = responseCode;
}
public String getResponse()
{
return response;
}
public int getResponseCode()
{
return responseCode;
}
}