System.out.println("executing request " + httpget.getRequestLine());
@Override public void cancelled() { logger.println(request.getRequestLine() + " cancelled"); } });
@Override public void failed(final Exception ex) { logger.println(request.getRequestLine() + "->" + ex); }
@Override public void completed(final HttpResponse response) { logger.println(request.getRequestLine() + "->" + response.getStatusLine()); }
public Integer getEventCount(){ try (CloseableHttpClient client = createHttpClient()) { final HttpGet request = new HttpGet(baseUrl + EVENT_COUNT_URI); request.addHeader("accept", "text/plain"); LOG.debug("Executing request " + request.getRequestLine()); try (CloseableHttpResponse response = client.execute(request)) { if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } String responseStr = EntityUtils.toString(response.getEntity()); LOG.debug("Response status: {}, entity: {}", response.getStatusLine(), responseStr); return Integer.parseInt(responseStr); } } catch (Exception e){ throw new RuntimeException("An Exception occurred while getting event count",e); } }
public Integer getEventCount(){ try (CloseableHttpClient client = createHttpClient()) { final HttpGet request = new HttpGet(baseUrl + EVENT_COUNT_URI); request.addHeader("accept", "text/plain"); LOG.debug("Executing request " + request.getRequestLine()); try (CloseableHttpResponse response = client.execute(request)) { if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } String responseStr = EntityUtils.toString(response.getEntity()); LOG.debug("Response status: {}, entity: {}", response.getStatusLine(), responseStr); return Integer.parseInt(responseStr); } } catch (Exception e){ throw new RuntimeException("An Exception occurred while getting event count",e); } }
CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpHost target = new HttpHost("localhost", 443, "https"); HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); RequestConfig config = RequestConfig.custom() .setProxy(proxy) .build(); HttpGet request = new HttpGet("/"); request.setConfig(config); System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); }
public static List<String> getQueues( String brokerIp, String username, String password, int managementPort) throws IOException { List<String> result = new ArrayList<>(); String encoding = Base64.encodeBase64String((username + ":" + password).getBytes()); HttpGet httpGet = new HttpGet("http://" + brokerIp + ":" + managementPort + "/api/queues/"); httpGet.setHeader("Authorization", "Basic " + encoding); log.trace("executing request " + httpGet.getRequestLine()); HttpClient httpclient = HttpClients.createDefault(); HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); InputStreamReader inputStreamReader = new InputStreamReader(entity.getContent()); JsonArray array = gson.fromJson(inputStreamReader, JsonArray.class); if (array != null) for (JsonElement queueJson : array) { String name = queueJson.getAsJsonObject().get("name").getAsString(); result.add(name); log.trace("found queue: " + name); } //TODO check for errors log.trace("found queues: " + result.toString()); return result; }
/** * (non-Javadoc) * * @see com.sematext.ag.sink.Sink#write(com.sematext.ag.Event) */ @Override public boolean write(SimpleSearchEvent event) { HttpGet httpGet = new HttpGet(solrUrl + SOLR_QUERY_TEMPLATE.replace("${QUERY_STRING}", event.getQueryString())); LOG.info("Sending Apache Solr search event " + httpGet.getRequestLine()); return execute(httpGet); } }
/** * (non-Javadoc) * * @see com.sematext.ag.sink.Sink#write(com.sematext.ag.Event) */ @Override public boolean write(SimpleSearchEvent event) { HttpGet httpGet = new HttpGet(esBaseUrl + ES_QUERY_TEMPLATE.replace("${INDEX_NAME}", indexName).replace("${QUERY_STRING}", event.getQueryString().replace(" ", "+"))); LOG.info("Sending ES search event " + httpGet.getRequestLine()); return execute(httpGet); } }
public class SharePointClientAuthentication { public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(AuthScope.ANY), new NTCredentials("username", "password", "https://hostname", "domain")); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build(); try { HttpGet httpget = new HttpGet("http://hostname/_api/web/lists"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } } }
CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("10.1.1.100", 8080), new UsernamePasswordCredentials("username", "password")); try (CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider).build()) { URL url = new URL(uri); HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); HttpHost proxy = new HttpHost("10.1.1.100", 8080); RequestConfig config = RequestConfig.custom().setProxy(proxy) .build(); HttpGet httpget = new HttpGet(url.getPath()); httpget.setConfig(config); System.out.println("Executing request " + httpget.getRequestLine() + " to " + target + " via " + proxy); try (CloseableHttpResponse response = httpclient.execute(target, httpget)) { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } } catch (IOException e1) { e1.printStackTrace(); }
private void setCredentials(String login, String password) { if (login != null && password != null) httpClient.getCredentialsProvider().setCredentials( new AuthScope(URL_HOST, 80), new UsernamePasswordCredentials(login, password)); } private InputStream execute(String url, String login, String password) { setCredentials(login, password); HttpGet get = new HttpGet(url); try { HttpResponse response = httpClient.execute(get); int code = response.getStatusLine().getStatusCode(); if (code == HttpURLConnection.HTTP_OK) { InputStream stream = response.getEntity().getContent(); return stream; } else { Log.e("TAG", "Wrong response code " + code + " for request " + get.getRequestLine().toString()); return null; } } catch (IOException e) { e.printStackTrace(); } return null; }
public class SharePointClientAuthentication { public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(AuthScope.ANY), new NTCredentials("username", "password", "https://hostname", "domain")); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build(); try { HttpGet httpget = new HttpGet("http://hostname/_api/web/lists"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } } }
DefaultHttpClient httpclient = new DefaultHttpClient(); try { httpclient.getCredentialsProvider().setCredentials( new AuthScope("localhost", 9009), new UsernamePasswordCredentials("username", "password*")); HttpGet httpget = new HttpGet("http://localhost:9009/path/list"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); System.out.println("executing request" + httpget.getRequestLine()); String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); System.out.println("Job Done!"); } catch (Exception ex) { Logger.getLogger(Command.class.getName()).log(Level.SEVERE, null, ex); } finally { httpclient.getConnectionManager().shutdown(); } }
HttpGet httpget = new HttpGet("http://localhost/"); System.out.println("Executing request " + httpget.getRequestLine());
HttpGet dhttpget = new HttpGet(uri); System.out.println("executing request " + dhttpget.getRequestLine()); HttpResponse dresponse = dhttpclient.execute(dhttpget);
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); HttpGet httpget = new HttpGet("https://localhost/protected"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpClientContext localContext = HttpClientContext.create(); // Bind custom cookie store to the local context localContext.setCookieStore(cookieStore); HttpGet httpget = new HttpGet("http://localhost/"); System.out.println("Executing request " + httpget.getRequestLine()); // Pass local context as a parameter CloseableHttpResponse response = httpclient.execute(httpget, localContext); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } EntityUtils.consume(response.getEntity()); } finally { response.close(); }
private void sendRequestThruProxy() throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext; sslContext = SSLContexts.custom().loadTrustMaterial((chain, authType) -> true).build(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new String[] {"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE); try (CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(sslConnectionSocketFactory) .build()) { HttpHost target = new HttpHost(BurpClientIT.TARGET_HOST); HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT, PROXY_SCHEME); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("/"); request.setConfig(config); log.info("Executing request {} to {} via {} proxy", request.getRequestLine(), target.toString(), proxy.toString()); httpClient.execute(target, request); } } }