@@ -92,10 +92,11 @@ public static Optional<JsonElement> getJson(@NotNull String userAgent, @NotNull
92
92
* @param data the {@link JsonObject JSON data} to send with the POST request
93
93
* @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
94
94
*
95
- * @return the response code of the request
95
+ * @return a {@link Response} object containing the response code and message, or null if the request failed
96
96
*/
97
- public static int postJson (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable JsonElement data , @ Nullable Consumer <HttpURLConnection > connectionConsumer ) {
98
- int responseCode = -1 ;
97
+ @ Nullable
98
+ public static Response postJson (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable JsonElement data , @ Nullable Consumer <HttpURLConnection > connectionConsumer ) {
99
+ Response response = null ;
99
100
HttpURLConnection connection = null ;
100
101
try {
101
102
connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
@@ -105,12 +106,12 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
105
106
connection .setDoOutput (true );
106
107
if (connectionConsumer != null ) connectionConsumer .accept (connection );
107
108
if (data != null ) connection .getOutputStream ().write (data .toString ().getBytes ());
108
- responseCode = connection .getResponseCode ();
109
+ response = new Response ( connection .getResponseCode (), connection . getResponseMessage () );
109
110
} catch (final Exception e ) {
110
111
if (DEBUG ) e .printStackTrace ();
111
112
}
112
113
if (connection != null ) connection .disconnect ();
113
- return responseCode ;
114
+ return response ;
114
115
}
115
116
116
117
/**
@@ -121,10 +122,11 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
121
122
* @param data the {@link JsonElement JSON data} to send with the PUT request
122
123
* @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
123
124
*
124
- * @return the response code of the request
125
+ * @return a {@link Response} object containing the response code and message, or null if the request failed
125
126
*/
126
- public static int putJson (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable JsonElement data , @ Nullable Consumer <HttpURLConnection > connectionConsumer ) {
127
- int responseCode = -1 ;
127
+ @ Nullable
128
+ public static Response putJson (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable JsonElement data , @ Nullable Consumer <HttpURLConnection > connectionConsumer ) {
129
+ Response response = null ;
128
130
HttpURLConnection connection = null ;
129
131
try {
130
132
connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
@@ -134,12 +136,12 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
134
136
connection .setDoOutput (true );
135
137
if (connectionConsumer != null ) connectionConsumer .accept (connection );
136
138
if (data != null ) connection .getOutputStream ().write (data .toString ().getBytes ());
137
- responseCode = connection .getResponseCode ();
139
+ response = new Response ( connection .getResponseCode (), connection . getResponseMessage () );
138
140
} catch (final Exception e ) {
139
141
if (DEBUG ) e .printStackTrace ();
140
142
}
141
143
if (connection != null ) connection .disconnect ();
142
- return responseCode ;
144
+ return response ;
143
145
}
144
146
145
147
/**
@@ -149,22 +151,23 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
149
151
* @param urlString the URL to send the DELETE request to
150
152
* @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
151
153
*
152
- * @return the response code of the request
154
+ * @return a {@link Response} object containing the response code and message, or null if the request failed
153
155
*/
154
- public static int delete (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable Consumer <HttpURLConnection > connectionConsumer ) {
155
- int responseCode = -1 ;
156
+ @ Nullable
157
+ public static Response delete (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable Consumer <HttpURLConnection > connectionConsumer ) {
158
+ Response response = null ;
156
159
HttpURLConnection connection = null ;
157
160
try {
158
161
connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
159
162
connection .setRequestMethod ("DELETE" );
160
163
connection .setRequestProperty ("User-Agent" , userAgent );
161
164
if (connectionConsumer != null ) connectionConsumer .accept (connection );
162
- responseCode = connection .getResponseCode ();
165
+ response = new Response ( connection .getResponseCode (), connection . getResponseMessage () );
163
166
} catch (final Exception e ) {
164
167
if (DEBUG ) e .printStackTrace ();
165
168
}
166
169
if (connection != null ) connection .disconnect ();
167
- return responseCode ;
170
+ return response ;
168
171
}
169
172
170
173
/**
@@ -175,4 +178,29 @@ public static int delete(@NotNull String userAgent, @NotNull String urlString, @
175
178
private HttpUtility () {
176
179
throw new UnsupportedOperationException ("This is a utility class and cannot be instantiated" );
177
180
}
181
+
182
+ /**
183
+ * Represents the response from an HTTP request
184
+ */
185
+ public static class Response {
186
+ /**
187
+ * The HTTP response code
188
+ */
189
+ public final int code ;
190
+ /**
191
+ * The HTTP response message
192
+ */
193
+ @ Nullable public final String message ;
194
+
195
+ /**
196
+ * Constructs a new {@link Response} instance
197
+ *
198
+ * @param code {@link #code}
199
+ * @param message {@link #message}
200
+ */
201
+ public Response (int code , @ Nullable String message ) {
202
+ this .code = code ;
203
+ this .message = message ;
204
+ }
205
+ }
178
206
}
0 commit comments