@@ -92,10 +92,11 @@ public static Optional<JsonElement> getJson(@NotNull String userAgent, @NotNull
9292 * @param data the {@link JsonObject JSON data} to send with the POST request
9393 * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
9494 *
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
9696 */
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 ;
99100 HttpURLConnection connection = null ;
100101 try {
101102 connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
@@ -105,12 +106,12 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
105106 connection .setDoOutput (true );
106107 if (connectionConsumer != null ) connectionConsumer .accept (connection );
107108 if (data != null ) connection .getOutputStream ().write (data .toString ().getBytes ());
108- responseCode = connection .getResponseCode ();
109+ response = new Response ( connection .getResponseCode (), connection . getResponseMessage () );
109110 } catch (final Exception e ) {
110111 if (DEBUG ) e .printStackTrace ();
111112 }
112113 if (connection != null ) connection .disconnect ();
113- return responseCode ;
114+ return response ;
114115 }
115116
116117 /**
@@ -121,10 +122,11 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
121122 * @param data the {@link JsonElement JSON data} to send with the PUT request
122123 * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
123124 *
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
125126 */
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 ;
128130 HttpURLConnection connection = null ;
129131 try {
130132 connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
@@ -134,12 +136,12 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
134136 connection .setDoOutput (true );
135137 if (connectionConsumer != null ) connectionConsumer .accept (connection );
136138 if (data != null ) connection .getOutputStream ().write (data .toString ().getBytes ());
137- responseCode = connection .getResponseCode ();
139+ response = new Response ( connection .getResponseCode (), connection . getResponseMessage () );
138140 } catch (final Exception e ) {
139141 if (DEBUG ) e .printStackTrace ();
140142 }
141143 if (connection != null ) connection .disconnect ();
142- return responseCode ;
144+ return response ;
143145 }
144146
145147 /**
@@ -149,22 +151,23 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
149151 * @param urlString the URL to send the DELETE request to
150152 * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
151153 *
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
153155 */
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 ;
156159 HttpURLConnection connection = null ;
157160 try {
158161 connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
159162 connection .setRequestMethod ("DELETE" );
160163 connection .setRequestProperty ("User-Agent" , userAgent );
161164 if (connectionConsumer != null ) connectionConsumer .accept (connection );
162- responseCode = connection .getResponseCode ();
165+ response = new Response ( connection .getResponseCode (), connection . getResponseMessage () );
163166 } catch (final Exception e ) {
164167 if (DEBUG ) e .printStackTrace ();
165168 }
166169 if (connection != null ) connection .disconnect ();
167- return responseCode ;
170+ return response ;
168171 }
169172
170173 /**
@@ -175,4 +178,29 @@ public static int delete(@NotNull String userAgent, @NotNull String urlString, @
175178 private HttpUtility () {
176179 throw new UnsupportedOperationException ("This is a utility class and cannot be instantiated" );
177180 }
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+ }
178206}
0 commit comments