comparison src/luan/modules/url/LuanUrl.java @ 1085:a04da9a3e9eb

improve url delete error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 19 Dec 2016 01:05:10 -0700
parents 6a87d51ae0ed
children 4aab4dd3ac9c
comparison
equal deleted inserted replaced
1084:aa967fd73b80 1085:a04da9a3e9eb
235 235
236 HttpURLConnection httpCon = (HttpURLConnection)con; 236 HttpURLConnection httpCon = (HttpURLConnection)con;
237 237
238 if( method==Method.DELETE ) { 238 if( method==Method.DELETE ) {
239 httpCon.setRequestMethod("DELETE"); 239 httpCon.setRequestMethod("DELETE");
240 return httpCon.getInputStream(); 240 return getInputStream(httpCon);
241 } 241 }
242 242
243 // POST 243 // POST
244 244
245 // httpCon.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 245 // httpCon.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
255 out = httpCon.getOutputStream(); 255 out = httpCon.getOutputStream();
256 out.write(post); 256 out.write(post);
257 } 257 }
258 out.flush(); 258 out.flush();
259 try { 259 try {
260 try { 260 return getInputStream(httpCon);
261 return httpCon.getInputStream();
262 } catch(IOException e) {
263 InputStream is = httpCon.getErrorStream();
264 if( is == null )
265 throw e;
266 Reader in = new InputStreamReader(is);
267 String msg = Utils.readAll(in);
268 in.close();
269 throw new LuanException(msg,e);
270 }
271 } finally { 261 } finally {
272 out.close(); 262 out.close();
263 }
264 }
265
266 private static InputStream getInputStream(HttpURLConnection httpCon) throws IOException, LuanException {
267 try {
268 return httpCon.getInputStream();
269 } catch(IOException e) {
270 InputStream is = httpCon.getErrorStream();
271 if( is == null )
272 throw e;
273 Reader in = new InputStreamReader(is);
274 String msg = Utils.readAll(in);
275 in.close();
276 throw new LuanException(msg,e);
273 } 277 }
274 } 278 }
275 279
276 @Override public String to_string() { 280 @Override public String to_string() {
277 return url.toString(); 281 return url.toString();