public ResponseImpl( HttpRequest request ) { body = request.body(); contentType = request.contentType(); headers = request.headers(); code = request.code(); url = request.getConnection().getURL().toString(); request.disconnect(); }
/** * Start a 'POST' request to the given URL along with the query params * * @param baseUrl * @param params the query parameters to include as part of the baseUrl * @param encode true to encode the full URL * @return request * @see #append(CharSequence, Map) * @see #encode(CharSequence) */ public static HttpRequest post(final CharSequence baseUrl, final Map<?, ?> params, final boolean encode) { String url = append(baseUrl, params); return post(encode ? encode(url) : url); }
/** * Start a 'GET' request to the given URL along with the query params * * @param baseUrl * @param params The query parameters to include as part of the baseUrl * @param encode true to encode the full URL * @return request * @see #append(CharSequence, Map) * @see #encode(CharSequence) */ public static HttpRequest get(final CharSequence baseUrl, final Map<?, ?> params, final boolean encode) { String url = append(baseUrl, params); return get(encode ? encode(url) : url); }
/** * Get all parameters from header value in response * <p> * This will be all key=value pairs after the first ';' that are separated by * a ';' * * @param headerName * @return non-null but possibly empty map of parameter headers */ public Map<String, String> parameters(final String headerName) { return getParams(header(headerName)); }
private String doPost() { HttpRequest post = HttpRequest.post(url, params, encode) .headers(headers) .connectTimeout(connectTimeout) .readTimeout(readTimeout) .acceptGzipEncoding() .uncompress(true); setOptionalHeaders(post); if (!Strings.isNullOrEmpty(body)){ post.send(body); } if (ssl){ trustHttps(post); } return post.body(); }
private String doGet() { HttpRequest get = HttpRequest.get(url, params, encode) .headers(headers) .connectTimeout(connectTimeout) .readTimeout(readTimeout) .acceptGzipEncoding() .uncompress(true); if (ssl){ trustHttps(get); } setOptionalHeaders(get); return get.body(); }
/** * Share the stats to share URL. Must not be called if sharing is disabled. * shareEnabled check is not done to avoid double checking. */ private void shareStats() { try { int code = HttpRequest.post(shareURL) .connectTimeout(5000) .readTimeout(5000) .send(objectMapper.writer().writeValueAsString(stats).getBytes(Charsets.UTF_8)) .code(); if (code >= 400) { logger.info("sharing stats on {} failed. Response code: {}", shareURL, code); } } catch (Exception e) { logger.info("sharing stats on {} failed. Exception: {}", shareURL, e.getMessage()); } }
/** * download a file * @param url http url * @param output the output which downloaded content will fill into */ public static void download(String url, OutputStream output){ try { HttpRequest request = HttpRequest.get(url); if (request.ok()){ request.receive(output); } else { throw new HttpException("request isn't ok: " + request.body()); } } catch (Exception e){ throw new HttpException(e); } } }
HttpRequest req = HttpRequest.get("https://google.com"); req.trustAllCerts(); req.trustAllHosts(); //If you are having certificate problems int code = req.code(); String body = req.body(); Log.d("CODE:", String.valueOf(code)); Log.d("BODY:", body);
/** * This method sends a request to upload a monitoring rule to the monitoring manager. @param rule is the monitoring rule to be uploaded @return the response of the monitoring manager */ public String addMonitoringRule(String rule){ String url = address + "/" + version + "/monitoring-rules"; String response = null; try { response = HttpRequest.post(url).send(rule).body(); } catch (Exception e) { journal.log(Level.INFO, "Connection to the monitoring manager refused"); } return response; }
public static boolean subscribe(String email, String description, String location, boolean fullTime) { HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put(SUBSCRIPTION_EMAIL_PARAM, email); parameters.put(SUBSCRIPTION_DESCRIPTION_PARAM, description); parameters.put(SUBSCRIPTION_LOCATION_PARAM, location); parameters.put(SUBSCRIPTION_FULL_TIME_PARAM, String.valueOf(fullTime)); String response = HttpRequest.post(ApiConstants.EMAIL_SUBSCRIPTION_URL) .part(SUBSCRIPTION_EMAIL_PARAM, email) .part(SUBSCRIPTION_DESCRIPTION_PARAM, description) .part(SUBSCRIPTION_LOCATION_PARAM, location) .part(SUBSCRIPTION_FULL_TIME_PARAM, String.valueOf(fullTime)) .body(); return SUBSCRIPTION_OK_PARAM.equals(response); }
/** * This method sends a request to attach an observer to a specific metric * @param callback the address on which the observer is running * * @param metric the requested metric */ public void attachObserver(String callback, String metric) { String url = address + "/" + version + "/metrics/" + metric + "/observers"; try { HttpRequest.post(url).send(callback).code(); journal.log(Level.INFO, "Observer attached"); } catch (Exception e) { journal.log(Level.INFO, "Connection to the monitoring manager refused"); } }
public User getUser(String username) { String url = String.format(ApiConstants.API_URL, String.format(ApiConstants.GET_USER, username)); try { String response = HttpRequest.get(url).body(); // convert json to object Gson gson = new Gson(); return gson.fromJson(response, User.class); } catch (Exception e) { e.printStackTrace(); } return null; } }
HttpRequest request = HttpRequest.post(url); request.authorization("Basic "+ah); request.part("file", fName+".png", "image/png", new File(file)); request.part("title", "test"); if(request.code()==201) { StringWriter sw = new StringWriter(); request.receive(sw); onMedia(Media.parse(new JsonParser().parse(sw.toString()).getAsJsonObject())); }
public String getToken(String code) throws JsonProcessingException, IOException { String body = HttpRequest.post(ACCESS_TOKEN_URL, ImmutableMap.of( "client_id", config.getGithubClientId(), "client_secret", config.getGithubClientSecret(), "code", code ), false).header("Accept", "application/json").body(); JsonNode node = om.readTree(body); if (node.has("access_token")) { String token = node.get("access_token").asText(); return token; } else { throw new AppException(Error.AUTH2_CODE_ERROR, body); } }
/** * Write part of a multipart request to the request body * * @param name * @param part * @return this request */ public HttpRequest part(final String name, final String part) { return part(name, null, part); }
@Override protected HttpRequest dealWithRequestBeforeSend(HttpRequest request) { super.dealWithRequestBeforeSend(request); // request.getConnection().addRequestProperty("SOAPAction", SOAP_ACTION); String query = buildSoapXml(); request.contentType("text/xml;charset=UTF-8").send(query); return request; }
/** * Is the response code a 200 OK? * * @return true if 200, false otherwise * @throws HttpRequestException */ public boolean ok() throws HttpRequestException { return HTTP_OK == code(); }