22
33import java .io .BufferedReader ;
44import java .io .DataOutputStream ;
5+ import java .io .IOException ;
56import java .io .InputStreamReader ;
67import java .net .HttpURLConnection ;
78import java .net .URL ;
@@ -22,142 +23,141 @@ public abstract class HttpRequests {
2223 * @param url URL
2324 * @param out StringBuffer to store response in
2425 * @return Response code
25- * @throws Exception Exception Occuring during the request
2626 */
27- public static int sendGet (String url , StringBuffer out ) throws Exception {
27+ public static int sendGet (String url , StringBuffer out ) {
2828 return sendWithoutBody (url , "GET" , out , null );
2929 }
3030
3131 /**
3232 * GET request.
3333 *
34- * @param url URL
35- * @param out StringBuffer to store response in
34+ * @param url URL
35+ * @param out StringBuffer to store response in
3636 * @param headers HTTP headers
3737 * @return Response code
38- * @throws Exception Exception Occuring during the request
3938 */
40- public static int sendGet (String url , StringBuffer out , List <Header > headers ) throws Exception {
39+ public static int sendGet (String url , StringBuffer out , List <Header > headers ) {
4140 return sendWithoutBody (url , "GET" , out , headers );
4241 }
4342
4443 /**
4544 * DELETE request.
4645 *
47- * @param url URL
48- * @param out StringBuffer to store response in
46+ * @param url URL
47+ * @param out StringBuffer to store response in
4948 * @param headers HTTP headers
5049 * @return Response code
51- * @throws Exception Exception Occuring during the request
5250 */
53- public static int sendDelete (String url , StringBuffer out , List <Header > headers ) throws Exception {
51+ public static int sendDelete (String url , StringBuffer out , List <Header > headers ) {
5452 return sendWithoutBody (url , "DELETE" , out , headers );
5553 }
5654
5755 /**
5856 * Send a request without body (GET, DELETE...)
5957 *
60- * @param url URL
61- * @param method HTTP method
62- * @param out StringBuffer to store response in
58+ * @param url URL
59+ * @param method HTTP method
60+ * @param out StringBuffer to store response in
6361 * @param headers HTTP headers
6462 * @return Response code
65- * @throws Exception Occuring during the request
6663 */
67- private static int sendWithoutBody (String url , String method , StringBuffer out , List <Header > headers ) throws Exception {
68-
69- URL obj = new URL (url );
70- HttpURLConnection con = (HttpURLConnection ) obj .openConnection ();
71-
72- con .setRequestMethod (method );
73- if (headers != null ) {
74- for (Header header : headers ) {
75- con .setRequestProperty (header .getKey (), header .getValue ());
64+ private static int sendWithoutBody (String url , String method , StringBuffer out , List <Header > headers ) {
65+ int responseCode = -1 ;
66+ try {
67+ URL obj = new URL (url );
68+ HttpURLConnection con = (HttpURLConnection ) obj .openConnection ();
69+
70+ con .setRequestMethod (method );
71+ if (headers != null ) {
72+ for (Header header : headers ) {
73+ con .setRequestProperty (header .getKey (), header .getValue ());
74+ }
7675 }
77- }
7876
79- int responseCode = con .getResponseCode ();
77+ responseCode = con .getResponseCode ();
8078
81- BufferedReader in = new BufferedReader (
82- new InputStreamReader (con .getInputStream ()));
83- String inputLine ;
79+ BufferedReader in = new BufferedReader (
80+ new InputStreamReader (con .getInputStream ()));
81+ String inputLine ;
8482
85- while ((inputLine = in .readLine ()) != null ) {
86- out .append (inputLine );
83+ while ((inputLine = in .readLine ()) != null ) {
84+ out .append (inputLine );
85+ }
86+ in .close ();
87+ } catch (IOException e ) {
88+ e .printStackTrace ();
8789 }
88- in .close ();
89-
9090 return responseCode ;
9191 }
9292
9393 /**
9494 * PUT request.
9595 *
96- * @param url URL
97- * @param out StringBuffer to store response in
98- * @param body Request body
96+ * @param url URL
97+ * @param out StringBuffer to store response in
98+ * @param body Request body
9999 * @param headers HTTP headers
100100 * @return Response code
101- * @throws Exception Occuring during the request
102101 */
103- public static int sendPut (String url , StringBuffer out , String body , List <Header > headers ) throws Exception {
102+ public static int sendPut (String url , StringBuffer out , String body , List <Header > headers ) {
104103 return sendWithBody (url , "PUT" , out , body , headers );
105104 }
106105
107106 /**
108107 * POST request.
109108 *
110- * @param url URL
111- * @param out StringBuffer to store response in
112- * @param body Request body
109+ * @param url URL
110+ * @param out StringBuffer to store response in
111+ * @param body Request body
113112 * @param headers HTTP headers
114113 * @return Response code
115- * @throws Exception Occuring during the request
116114 */
117- public static int sendPost (String url , StringBuffer out , String body , List <Header > headers ) throws Exception {
115+ public static int sendPost (String url , StringBuffer out , String body , List <Header > headers ) {
118116 return sendWithBody (url , "POST" , out , body , headers );
119117 }
120118
121119 /**
122120 * Send a request with a body (POST, PUT...).
123121 *
124- * @param url URL
125- * @param method HTTP method
126- * @param out StringBuffer to store response in
127- * @param body Request body
122+ * @param url URL
123+ * @param method HTTP method
124+ * @param out StringBuffer to store response in
125+ * @param body Request body
128126 * @param headers HTTP headers
129127 * @return Response code
130- * @throws Exception Occuring during the request
131128 */
132- private static int sendWithBody (String url , String method , StringBuffer out , String body , List <Header > headers ) throws Exception {
133-
134- URL obj = new URL (url );
135- HttpURLConnection con = (HttpURLConnection ) obj .openConnection ();
136-
137- con .setRequestMethod (method );
138- if (headers != null ) {
139- for (Header header : headers ) {
140- con .setRequestProperty (header .getKey (), header .getValue ());
129+ private static int sendWithBody (String url , String method , StringBuffer out , String body , List <Header > headers ) {
130+ int responseCode = -1 ;
131+ try {
132+ URL obj = new URL (url );
133+ HttpURLConnection con = (HttpURLConnection ) obj .openConnection ();
134+
135+ con .setRequestMethod (method );
136+ if (headers != null ) {
137+ for (Header header : headers ) {
138+ con .setRequestProperty (header .getKey (), header .getValue ());
139+ }
141140 }
142- }
143141
144- con .setDoOutput (true );
145- DataOutputStream wr = new DataOutputStream (con .getOutputStream ());
146- wr .writeBytes (body );
147- wr .flush ();
148- wr .close ();
142+ con .setDoOutput (true );
143+ DataOutputStream wr = new DataOutputStream (con .getOutputStream ());
144+ wr .writeBytes (body );
145+ wr .flush ();
146+ wr .close ();
149147
150- int responseCode = con .getResponseCode ();
148+ responseCode = con .getResponseCode ();
151149
152- BufferedReader in = new BufferedReader (
153- new InputStreamReader (con .getInputStream ()));
154- String inputLine ;
150+ BufferedReader in = new BufferedReader (
151+ new InputStreamReader (con .getInputStream ()));
152+ String inputLine ;
155153
156- while ((inputLine = in .readLine ()) != null ) {
157- out .append (inputLine );
154+ while ((inputLine = in .readLine ()) != null ) {
155+ out .append (inputLine );
156+ }
157+ in .close ();
158+ } catch (IOException e ) {
159+ e .printStackTrace ();
158160 }
159- in .close ();
160-
161161 return responseCode ;
162162 }
163163
0 commit comments