Refine search
HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://foo.com/bar"); httpGet.addHeader(BasicScheme.authenticate( new UsernamePasswordCredentials("user", "password"), "UTF-8", false)); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity responseEntity = httpResponse.getEntity(); // read the stream returned by responseEntity.getContent()
public HttpResponse makeRequestAndGetResponse(String url, Map<String, String> headers) { HttpResponse response = null; try { HttpGet getRequest = new HttpGet(url); // add all headers for (Entry<String, String> header : headers.entrySet()) { getRequest.addHeader(header.getKey(), header.getValue()); } response = httpClient.execute(getRequest); getRequest.reset(); } catch (Exception e) { throw new RuntimeException(e); } return response; }
public static InputStream getData(String url, String requestHeaderName, String requestHeaderValue) throws ClientProtocolException, IOException { HttpGet httpget = new HttpGet(url); if (requestHeaderValue != null) { httpget.addHeader(requestHeaderName, requestHeaderValue); } HttpClient httpclient = getClient(); HttpResponse response = httpclient.execute(httpget); if(response.getStatusLine().getStatusCode() != 200) return null; HttpEntity entity = response.getEntity(); if (entity == null) { return null; } return entity.getContent(); }
private JSONObject readJSONFromUrl(String urlString, Map<String, String> headers) throws IOException, JSONException { HttpClient httpClient = openConnection(); HttpGet request = new HttpGet(urlString); for (Map.Entry<String, String> entry : headers.entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(request); InputStream content = response.getEntity().getContent(); return readAllIntoJSONObject(content); }
private JSONObject readJSONObjectFromUrl(String urlString, LivySessionService livySessionService, Map<String, String> headers) throws IOException, JSONException, SessionManagerException { HttpClient httpClient = livySessionService.getConnection(); HttpGet request = new HttpGet(urlString); for (Map.Entry<String, String> entry : headers.entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(request); InputStream content = response.getEntity().getContent(); return readAllIntoJSONObject(content); }
public String fetchApiVersionFromServer(String baseUrl, ApacheHttpClientDelegate delegate) throws IOException { HttpGet get = new HttpGet(baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "version"); get.addHeader(HttpHeaders.ACCEPT, "*/*"); get.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); CloseableHttpResponse response = delegate.getHttpClient().execute(get); return response.getFirstHeader("Api-Version") != null ? response.getFirstHeader("Api-Version").getValue() : API_VERSION; } }
private String getHttpResponse(String url) { DefaultHttpClient client = new DefaultHttpClient(); String msg = null; int retry_times = 0; while (msg == null && retry_times < HTTP_RETRY) { retry_times++; HttpGet request = new HttpGet(url); try { request.addHeader("accept", "application/json"); HttpResponse response = client.execute(request); msg = EntityUtils.toString(response.getEntity()); } catch (Exception e) { logger.warn("Failed to fetch http response. Retry={}", retry_times, e); } finally { request.releaseConnection(); } } return msg; }
@Override public String call() throws Exception { try { if ( log.isBasic() ) { log.logBasic( BaseMessages.getString( PKG, "PurRepositoryConnector.SessionService.Start" ) ); } CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( username, password ); provider.setCredentials( AuthScope.ANY, credentials ); HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider( provider ).build(); HttpGet method = new HttpGet( repositoryMeta.getRepositoryLocation().getUrl() + "/api/session/userName" ); if ( StringUtils.isNotBlank( System.getProperty( "pentaho.repository.client.attemptTrust" ) ) ) { method.addHeader( TRUST_USER, username ); } HttpResponse response = client.execute( method ); if ( log.isBasic() ) { log.logBasic( BaseMessages.getString( PKG, "PurRepositoryConnector.SessionService.Sync" ) ); //$NON-NLS-1$ } return EntityUtils.toString( response.getEntity() ); } catch ( Exception e ) { if ( log.isError() ) { log.logError( BaseMessages.getString( PKG, "PurRepositoryConnector.Error.EnableToGetUser" ), e ); } return null; } } } );
final HttpGet get = new HttpGet(url); get.setConfig(requestConfigBuilder.build()); final String lastModified = beforeStateMap.get(LAST_MODIFIED + ":" + url); if (lastModified != null) { get.addHeader(HEADER_IF_MODIFIED_SINCE, parseStateValue(lastModified).getValue()); get.addHeader(HEADER_IF_NONE_MATCH, parseStateValue(etag).getValue()); get.addHeader(HEADER_ACCEPT, accept); customHeaderValue = context.getProperty(customProperty).evaluateAttributeExpressions(); if (StringUtils.isNotBlank(customHeaderValue.getValue())) { get.addHeader(customProperty.getName(), customHeaderValue.getValue());
public String getRESTJSONResults(URL endpointUrl, Map<String, String> getArgs) throws IOException, URISyntaxException { String encodedParams = encodeParams(getArgs); HttpGet get = new HttpGet(appendToUrl(endpointUrl, encodedParams).toURI()); get.addHeader("accept", "application/json"); HttpResponse response = CLIENT.get().execute(get); if (response.getStatusLine().getStatusCode() != 200) { throw new IllegalStateException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } return new BufferedReader(new InputStreamReader(response.getEntity().getContent())) .lines().collect(Collectors.joining("\n")); } public URL appendToUrl(URL endpointUrl, String params) throws MalformedURLException {
public HttpGet createHttpGet(URL url) { HttpGet httpGet = new HttpGet(url.toString()); httpGet.addHeader("User-Agent", "Mozilla/5.0"); httpGet.addHeader("Connection", "keep-alive"); int timeout = (int) SECONDS.toMillis(config.getTimeout()); RequestConfig requestConfig = custom().setCookieSpec(STANDARD) .setSocketTimeout(timeout).build(); httpGet.setConfig(requestConfig); return httpGet; }
/** * Gets the response. * * @param q * the q * @param _id * the _id * @return the response * @throws URISyntaxException * the URI syntax exception * @throws IOException * Signals that an I/O exception has occurred. * @throws ClientProtocolException * the client protocol exception */ private HttpResponse getResponse(StringBuilder q, String _id) throws URISyntaxException, IOException, ClientProtocolException { URI uri = new URI(CouchDBConstants.PROTOCOL, null, httpHost.getHostName(), httpHost.getPort(), _id, q.toString(), null); HttpGet get = new HttpGet(uri); get.addHeader("Accept", "application/json"); return httpClient.execute(httpHost, get, CouchDBUtils.getContext(httpHost)); }