@Override public Delete doConnect() { try { connection.setDoOutput(true); connection.setRequestMethod("DELETE"); connection.setInstanceFollowRedirects(redirect); connection.connect(); return this; } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
public Request(String url, int connectTimeout, int readTimeout) { try { this.url = url; connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(connectTimeout); connection.setReadTimeout(readTimeout); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Returns input stream to read server response from. * * @return input stream to read server response from. */ public InputStream getInputStream() { try { return connection.getInputStream(); }catch(SocketTimeoutException e){ throw new HttpException("Failed URL: " + url + ", waited for: " + connection.getConnectTimeout() + " milliseconds", e); }catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
@Override public Get doConnect() { try { connection.setDoInput(true); connection.setDoOutput(true); connection.setInstanceFollowRedirects(redirect); connection.setUseCaches(false); connection.setRequestMethod("GET"); return this; } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Returns HTTP response code. * * @return HTTP response code. */ public int responseCode() { try { connect(); return connection.getResponseCode(); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
@Override public Put doConnect() { try { connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("PUT"); connection.setInstanceFollowRedirects(redirect); OutputStream os = connection.getOutputStream(); os.write(content); os.flush(); return this; } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Returns response message from server, such as "OK", or "Created", etc. * * @return response message from server, such as "OK", or "Created", etc. */ public String responseMessage() { try { connect(); return connection.getResponseMessage(); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
@Override public Patch doConnect() { try { this.connection.setDoInput(true); this.connection.setDoOutput(true); this.connection.setUseCaches(false); this.connection.setRequestMethod("POST"); this.connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); OutputStream out = this.connection.getOutputStream(); out.write(this.content); out.flush(); return this; } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } } }
/** * Executes a PUT request. * * @param url url of resource. * @param content content to be "put" * @param connectTimeout connection timeout in milliseconds. * @param readTimeout read timeout in milliseconds. * @return {@link Put} object. */ public static Put put(String url, byte[] content, int connectTimeout, int readTimeout) { try { return new Put(url, content, connectTimeout, readTimeout); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Executes a GET request * * @param url url of resource. * @param connectTimeout connection timeout in milliseconds. * @param readTimeout read timeout in milliseconds. * @return {@link Get} object. */ public static Get get(String url, int connectTimeout, int readTimeout) { try { return new Get(url, connectTimeout, readTimeout); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Executes a DELETE request. * * @param url url of resource to delete * @param connectTimeout connection timeout in milliseconds. * @param readTimeout read timeout in milliseconds. * @return {@link Delete} */ public static Delete delete(String url, int connectTimeout, int readTimeout) { try { return new Delete(url, connectTimeout, readTimeout); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Executes a POST request. * * @param url url of resource. * @param content content to be posted. * @param connectTimeout connection timeout in milliseconds. * @param readTimeout read timeout in milliseconds. * @return {@link Post} object. */ public static Post post(String url, byte[] content, int connectTimeout, int readTimeout) { try { return new Post(url, content, connectTimeout, readTimeout); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Executes a PATCH request. * * @param url url of resource. * @param content content to be posted. * @param connectTimeout connection timeout in milliseconds. * @param readTimeout read timeout in milliseconds. * @return {@link Patch} object. */ public static Patch patch(String url, byte[] content, int connectTimeout, int readTimeout) { try { return new Patch(url, content, connectTimeout, readTimeout); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Executes a POST request. Often used to post form parameters: * * <pre> * Http.post("http://example.com/create").param("name1", "val1"); * </pre> * * @param url url of resource. * @param connectTimeout connection timeout in milliseconds. * @param readTimeout read timeout in milliseconds. * @return {@link Post} object. */ public static Post post(String url, int connectTimeout, int readTimeout) { try { return new Post(url, null, connectTimeout, readTimeout); } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
@Override protected Multipart doConnect() { try { boundary = "JavaLite-HTTP-"+ UUID.randomUUID() ; connection.setUseCaches(false); connection.setDoOutput(true); // indicates POST method connection.setDoInput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); connection.setInstanceFollowRedirects(redirect); outputStream = connection.getOutputStream(); writer = new PrintWriter(new OutputStreamWriter(outputStream), true); sendData(); finish(); return this; } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
@Override public Post doConnect() { try { connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setInstanceFollowRedirects(redirect); if(params.size() > 0){ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } OutputStream out = connection.getOutputStream(); if(params.size() > 0){ out.write(Http.map2Content(params).getBytes()); } if(content != null){ out.write(content); } out.flush(); return this; } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); } }
throw new HttpException("failed to generate content from map", e);
private void sendFile(FileField f) { try { String fileName = f.getFile().getName(); writer.append("--").append(boundary).append(LINE_FEED); writer.append("Content-Disposition: form-data; name=\"").append(f.getName()).append("\"; filename=\"").append(fileName).append("\"").append(LINE_FEED); writer.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED); writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED); writer.append(LINE_FEED); writer.flush(); FileInputStream inputStream = new FileInputStream(f.getFile()); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); inputStream.close(); writer.append(LINE_FEED); writer.flush(); } catch (Exception e) { throw new HttpException(e); } }
/** * Fetches response content from server as bytes. * * @return response content from server as bytes. */ public byte[] bytes() { connect(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int count; try { InputStream in = connection.getInputStream(); while ((count = in.read(bytes)) != -1) { bout.write(bytes, 0, count); } } catch (Exception e) { throw new HttpException("Failed URL: " + url, e); }finally { dispose(); } return bout.toByteArray(); }
/** * Fetches response content from server as String. * * @return response content from server as String. */ public String text() { try { connect(); return responseCode() >= 400 ? read(connection.getErrorStream()) : read(connection.getInputStream()); } catch (IOException e) { throw new HttpException("Failed URL: " + url, e); }finally { dispose(); } }