22
22
* Utility class for making HTTP requests
23
23
*/
24
24
public class HttpUtility {
25
+ public static final boolean DEBUG = false ;
26
+
25
27
/**
26
28
* Sends a GET request to the specified URL and returns the result of the specified function
27
29
*
@@ -43,10 +45,13 @@ public static <T> Optional<T> get(@NotNull String userAgent, @NotNull String url
43
45
connection .setRequestMethod ("GET" );
44
46
connection .setRequestProperty ("User-Agent" , userAgent );
45
47
if (connectionConsumer != null ) connectionConsumer .accept (connection );
46
- if (connection .getResponseCode () == 404 ) return Optional .empty ();
48
+ if (connection .getResponseCode () == 404 ) {
49
+ if (DEBUG ) System .out .println ("[JU] 404: " + url );
50
+ return Optional .empty ();
51
+ }
47
52
result = function .apply (new InputStreamReader (connection .getInputStream ()));
48
- } catch (final IOException ignored ) {
49
- // Ignored
53
+ } catch (final IOException e ) {
54
+ if ( DEBUG ) e . printStackTrace ();
50
55
}
51
56
if (connection != null ) connection .disconnect ();
52
57
return Optional .ofNullable (result );
@@ -102,8 +107,8 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
102
107
if (connectionConsumer != null ) connectionConsumer .accept (connection );
103
108
connection .getOutputStream ().write (data .toString ().getBytes ());
104
109
responseCode = connection .getResponseCode ();
105
- } catch (final IOException ignored ) {
106
- // Ignored
110
+ } catch (final IOException e ) {
111
+ if ( DEBUG ) e . printStackTrace ();
107
112
}
108
113
if (connection != null ) connection .disconnect ();
109
114
return responseCode ;
@@ -131,8 +136,8 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
131
136
if (connectionConsumer != null ) connectionConsumer .accept (connection );
132
137
connection .getOutputStream ().write (data .toString ().getBytes ());
133
138
responseCode = connection .getResponseCode ();
134
- } catch (final IOException ignored ) {
135
- // Ignored
139
+ } catch (final IOException e ) {
140
+ if ( DEBUG ) e . printStackTrace ();
136
141
}
137
142
if (connection != null ) connection .disconnect ();
138
143
return responseCode ;
@@ -160,8 +165,8 @@ public static int patchJson(@NotNull String userAgent, @NotNull String urlString
160
165
if (connectionConsumer != null ) connectionConsumer .accept (connection );
161
166
connection .getOutputStream ().write (data .toString ().getBytes ());
162
167
responseCode = connection .getResponseCode ();
163
- } catch (final IOException ignored ) {
164
- // Ignored
168
+ } catch (final IOException e ) {
169
+ if ( DEBUG ) e . printStackTrace ();
165
170
}
166
171
if (connection != null ) connection .disconnect ();
167
172
return responseCode ;
@@ -185,8 +190,8 @@ public static int delete(@NotNull String userAgent, @NotNull String urlString, @
185
190
connection .setRequestProperty ("User-Agent" , userAgent );
186
191
if (connectionConsumer != null ) connectionConsumer .accept (connection );
187
192
responseCode = connection .getResponseCode ();
188
- } catch (final IOException ignored ) {
189
- // Ignored
193
+ } catch (final IOException e ) {
194
+ if ( DEBUG ) e . printStackTrace ();
190
195
}
191
196
if (connection != null ) connection .disconnect ();
192
197
return responseCode ;
0 commit comments