Refine search
String proxyPassword, String nonProxyHosts) { HttpClient client = new HttpClient(); method.getParams().setSoTimeout(timeout); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); if (httpHeaders != null) { for (String httpHeaderKey : httpHeaders.stringPropertyNames()) { method.addRequestHeader(new Header(httpHeaderKey, httpHeaders.getProperty(httpHeaderKey))); logger.debug("About to execute '{}'", method.getURI()); } catch (URIException e) { logger.debug("{}", e.getMessage()); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { logger.debug("Method failed: {}", method.getStatusLine()); String responseBody = IOUtils.toString(method.getResponseBodyAsStream()); if (!responseBody.isEmpty()) { logger.debug("{}", responseBody); logger.error("Fatal transport error: {}", ioe.toString()); } finally { method.releaseConnection();
public HttpMethod applyTo(HttpMethod method) { // Add headers to the request. for (Header header : headers) { method.setRequestHeader(header); } if (LOG.isDebugEnabled()) { LOG.debug("Added HTTP Headers to method: " + Arrays.toString(method.getRequestHeaders())); } return method; } }
try { LOGGER.info("Trying to send request: {}.", firstPutReqString); int firstResponseCode = _httpClient.executeMethod(firstPutReq); if (firstResponseCode != 307) { LOGGER.error(String.format("Failed to execute the first PUT request to upload segment to webhdfs: %s. " + "Expected response code 307, but get %s. Response body: %s", firstPutReqString, firstResponseCode, firstPutReq.getResponseBodyAsString())); return false; return false; } finally { firstPutReq.releaseConnection(); String redirectedReqString = firstPutReq.getResponseHeader(LOCATION).getValue(); PutMethod redirectedReq = new PutMethod(redirectedReqString); File localFile = new File(localFilePath); int redirectedResponseCode = _httpClient.executeMethod(redirectedReq); if (redirectedResponseCode != 201) { LOGGER.error(String.format("Failed to execute the redirected PUT request to upload segment to webhdfs: %s. "
protected static String createUrl(List<NameValuePair> pairs) throws URIException{ HttpMethod method = new GetMethod("http://example.org"); method.setQueryString(pairs.toArray(new NameValuePair[]{})); return method.getURI().getEscapedURI(); }
/** * Tests if the {@link HttpMethod method} requires a redirect to another location. * * @param method HTTP method * * @return boolean <tt>true</tt> if a retry is needed, <tt>false</tt> otherwise. */ private boolean isRedirectNeeded(final HttpMethod method) { switch (method.getStatusCode()) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: LOG.debug("Redirect required"); if (method.getFollowRedirects()) { return true; } else { return false; } default: return false; } //end of switch }
private InputStream setupInputStream(URL url, Map<String, String> headers) throws IOException { HttpClient client = new HttpClient(); String uri = url.toExternalForm(); if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, "URL is " + uri); HttpMethod get = new GetMethod(uri); if (MapUtils.isNotEmpty(headers)) { for (String headerName : headers.keySet()) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, "Adding header " + headerName + " = " + headers.get(headerName)); } get.addRequestHeader(headerName, headers.get(headerName)); } } int code = client.executeMethod(get); if (code != 200) { throw new IOException("Connection returned code " + code); } return get.getResponseBodyAsStream(); } }
private static int httpNotification(String uri) throws IOException { URI url = new URI(uri, false); HttpClient m_client = new HttpClient(); HttpMethod method = new GetMethod(url.getEscapedURI()); method.setRequestHeader("Accept", "*/*"); return m_client.executeMethod(method); }
private CryptoCurrency readCurrencyViaJson(String key) throws IOException { HttpMethod get = new GetMethod(restEndpoint + "/" + key); get.setRequestHeader("Accept", "application/json"); restClient.executeMethod(get); String json = get.getResponseBodyAsString(); JsonNode jsonNode = new ObjectMapper().readTree(json); JsonNode description = jsonNode.get("description"); JsonNode rank = jsonNode.get("rank"); return new CryptoCurrency(description.asText(), rank.intValue()); }
private String getHttpResponse(String url) throws IOException { HttpClient client = new HttpClient(); HttpMethod get = new GetMethod(url); try { client.executeMethod(get); Header h = get.getResponseHeader("Refresh"); if (h != null) { String s = h.getValue(); int cut = s.indexOf("url="); if (cut >= 0) { response = get.getResponseBodyAsString(); log.debug("Job " + mrJobId + " get status check result.\n"); } else { get.releaseConnection();
protected Object getSharesUpdateViaRest(String key) throws IOException, ClassNotFoundException { HttpMethod get = new GetMethod(DEFAULT_REST_URL + "/" + key); get.setRequestHeader("Accept", "application/x-java-serialized-object"); restClient.executeMethod(get); if (get.getStatusCode() == HttpStatus.SC_OK) { ObjectInputStream ois = new ObjectInputStream(get.getResponseBodyAsStream()); return ois.readObject(); } else { return null; } }
public static String getOverHttp(String url) { HttpClient client = new HttpClient(); String response = null; HttpMethod get = new GetMethod(url); try { client.executeMethod(get); response = get.getResponseBodyAsString(); } catch (Exception e) { e.printStackTrace(); } finally { get.releaseConnection(); } return response; }
break; case GET: http = (request.body() == null ? new GetMethod() : new GetMethodWithBody()); break; case POST: throw new EsHadoopInvalidRequest("URI has query portion on it: [" + uri + "]"); http.setURI(new URI(escapeUri(uri.toString(), sslEnabled), false)); http.setPath(path); uri = http.getURI().toString(); } catch (URIException uriex) { throw new EsHadoopTransportException("Invalid target URI " + request, uriex); http.setQueryString(params.toString()); client.executeMethod(http); } finally { stats.netTotalTime += (System.currentTimeMillis() - start); Socket sk = ReflectionUtils.invoke(GET_SOCKET, conn, (Object[]) null); String addr = sk.getLocalAddress().getHostAddress(); log.trace(String.format("Rx %s@[%s] [%s-%s] [%s]", proxyInfo, addr, http.getStatusCode(), HttpStatus.getStatusText(http.getStatusCode()), http.getResponseBodyAsString())); return new SimpleResponse(http.getStatusCode(), new ResponseInputStream(http), httpInfo);
Object read() throws IOException { HttpMethod get = new GetMethod(getEndpoint(this.cacheName) + "/" + this.key); if (this.accept != null) { get.setRequestHeader(ACCEPT, this.accept.toString()); } if (keyContentType != null) { get.setRequestHeader("Key-Content-Type", this.keyContentType); } restClient.executeMethod(get); assertEquals(get.getStatusCode(), HttpStatus.SC_OK); return get.getResponseBody(); } }
public String getKylinProperties() throws IOException { String url = baseUrl + "/admin/config"; HttpMethod request = new GetMethod(url); try { int code = client.executeMethod(request); String msg = Bytes.toString(request.getResponseBody()); JSONObject obj = new JSONObject(msg); msg = obj.getString("config"); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); return msg; } catch (JSONException e) { throw new IOException("Error when parsing json response from REST"); } finally { request.releaseConnection(); } }
private HttpClient setUpHttpClient(HttpMethod method) { HttpClient httpclient = new HttpClient(); httpclient.getParams().setAuthenticationPreemptive(true); httpclient.getState().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(this.user, this.password) ); method.setDoAuthentication(true); method.addRequestHeader("Content-Type", "application/json"); return httpclient; }
@Override public String getResponseHeader(final String headerName) { Header responseHeader = methodResponse.getResponseHeader(headerName); return responseHeader == null ? null : responseHeader.getValue(); }
throws RedirectException { Header locationHeader = method.getResponseHeader("location"); if (locationHeader == null) { LOG.error("Received redirect response " + method.getStatusCode() + " but no location header"); return false; currentUri = new URI( this.conn.getProtocol().getScheme(), null, this.conn.getHost(), this.conn.getPort(), method.getPath() ); String charset = method.getParams().getUriCharset(); redirectUri = new URI(location, true, charset); method.getParams().setDefaults(this.params); method.setURI(redirectUri); hostConfiguration.setHost(redirectUri); } catch (URIException ex) { method.getHostAuthState().invalidate(); return true;
throw new IllegalArgumentException("HttpMethod parameter may not be null"); HostConfiguration defaulthostconfig = getHostConfiguration(); if (hostconfig == null) { hostconfig = defaulthostconfig; URI uri = method.getURI(); if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) { if (uri.isAbsoluteURI()) { hostconfig.setHost(uri); getHttpConnectionManager(), hostconfig, this.params, (state == null ? getState() : state)); methodDirector.executeMethod(method); return method.getStatusCode();
private Map<String, String> executeAndGetHeaders(HttpMethod method) throws IOException { try { client.executeMethod(method); return Arrays.stream(method.getResponseHeaders()) .collect(Collectors.toMap(Header::getName, Header::getValue)); } catch (IOException e) { // probably either configuration error or IDEA is not running throw new IOException("Failed to communicate with JumpToCode", e); } finally { method.releaseConnection(); } }
public void wipeCache(String type, String action, String name) throws IOException { String url = baseUrl + "/cache/" + type + "/" + name + "/" + action; HttpMethod request = new PutMethod(url); try { int code = client.executeMethod(request); String msg = Bytes.toString(request.getResponseBody()); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); } catch (HttpException ex) { throw new IOException(ex); } finally { request.releaseConnection(); } }