private SystemDefaultHttpClient getHttpClient() { final SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient(); httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(true)); Credentials use_jaas_creds = new Credentials() { public String getPassword() { return null; } public Principal getUserPrincipal() { return null; } }; httpClient.getCredentialsProvider().setCredentials( AuthScope.ANY, use_jaas_creds); return httpClient; }
protected void _testAuthenticationHttpClient(Authenticator authenticator, boolean doPost) throws Exception { start(); try { SystemDefaultHttpClient httpClient = getHttpClient(); doHttpClientRequest(httpClient, new HttpGet(getBaseURL())); // Always do a GET before POST to trigger the SPNego negotiation if (doPost) { HttpPost post = new HttpPost(getBaseURL()); byte [] postBytes = POST.getBytes(); ByteArrayInputStream bis = new ByteArrayInputStream(postBytes); InputStreamEntity entity = new InputStreamEntity(bis, postBytes.length); // Important that the entity is not repeatable -- this means if // we have to renegotiate (e.g. b/c the cookie wasn't handled properly) // the test will fail. Assert.assertFalse(entity.isRepeatable()); post.setEntity(entity); doHttpClientRequest(httpClient, post); } } finally { stop(); } }
protected void _testAuthenticationHttpClient(Authenticator authenticator, boolean doPost) throws Exception { start(); try { SystemDefaultHttpClient httpClient = getHttpClient(); doHttpClientRequest(httpClient, new HttpGet(getBaseURL())); // Always do a GET before POST to trigger the SPNego negotiation if (doPost) { HttpPost post = new HttpPost(getBaseURL()); byte[] postBytes = POST.getBytes(); ByteArrayInputStream bis = new ByteArrayInputStream(postBytes); InputStreamEntity entity = new InputStreamEntity(bis, postBytes.length); // Important that the entity is not repeatable -- this means if // we have to renegotiate (e.g. b/c the cookie wasn't handled properly) // the test will fail. Assert.assertFalse(entity.isRepeatable()); post.setEntity(entity); doHttpClientRequest(httpClient, post); } } finally { stop(); } }
public static void publish(String url, String username, String password, String testCaseFolderName, String dataFileName) { log.info("Starting WSO2 HttpEventPublisher Client"); KeyStoreUtil.setTrustStoreParams(); HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); List<String> messagesList = readMsg(getTestDataFileLocation(testCaseFolderName, dataFileName)); for (String message : messagesList) { StringEntity entity = new StringEntity(message); log.info("Sending message:"); log.info(message + "\n"); method.setEntity(entity); if (url.startsWith("https")) { processAuthentication(method, username, password); } httpClient.execute(method).getEntity().getContent().close(); Thread.sleep(1000); } Thread.sleep(500); // Waiting time for the message to be sent } catch (Throwable t) { log.error("Error when sending the messages", t); } }
/** * Gets an image from a URL. * * @param url the url * @return the image from the url */ public BufferedImage getImageFromUrl(String url) { SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient(); try { HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); byte[] bytes = EntityUtils.toByteArray(entity); return ImageIO.read(new ByteArrayInputStream(bytes)); } else { throw new IOException("Download failed, HTTP response code " + statusCode + " - " + statusLine.getReasonPhrase()); } } catch (IOException e) { LOGGER.warn("Error loading image from \"" + url + "\". " + e.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } return null; }
/** * Loads TEI data from a URL. * * @return the resulting GlyphDefinition list */ public List<GlyphDefinition> loadDataFromUrl() { SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient(); try { HttpGet httpGet = new HttpGet(dataSource.getBasePath()); return httpClient.execute(httpGet, new XMLResponseHandler()); } catch (IOException e) { String message = String.format( i18n.getString("TeiLoadWorker.couldNotLoadData"), e.getLocalizedMessage(), dataSource.getBasePath()); if (e instanceof UnknownHostException) { message += " Unknown host"; } JOptionPane.showMessageDialog( null, message, i18n.getString("TeiLoadWorker.error"), JOptionPane.ERROR_MESSAGE); LOGGER.info(e); } finally { httpClient.getConnectionManager().shutdown(); } return null; }
public DefaultHttpClientImpl() { cm = new PoolingClientConnectionManager(); cm.setMaxTotal(100); cm.setDefaultMaxPerRoute(100); client = new SystemDefaultHttpClient() { @Override protected ClientConnectionManager createClientConnectionManager() { return cm; } }; client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000); client.getParams().setParameter(CoreConnectionPNames.TCP_NODELAY, true); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000); }
public DefaultWmsHttpClientFactory() { final PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); cm.setMaxTotal(100); cm.setDefaultMaxPerRoute(100); client = new SystemDefaultHttpClient() { @Override protected ClientConnectionManager createClientConnectionManager() { return cm; } }; client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000); client.getParams().setParameter(CoreConnectionPNames.TCP_NODELAY, true); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000); }
public static void main(String[] args) { HttpClient client1 = new DefaultHttpClient(); // BAD HttpClient client2 = new SystemDefaultHttpClient(); // OK try { SSLContext context1 = SSLContext.getInstance("SSL"); // BAD SSLContext context2 = SSLContext.getInstance("TLS"); // OK } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Creates new http client by using the provided configuration. * * @param params * http client configuration, if null a client with default * configuration (no additional configuration) is created. */ public static HttpClient createClient(final SolrParams params) { final ModifiableSolrParams config = new ModifiableSolrParams(params); if (logger.isDebugEnabled()) { logger.debug("Creating new http client, config:" + config); } final DefaultHttpClient httpClient = new SystemDefaultHttpClient(); configureClient(httpClient, config); return httpClient; }
public void receive(String url, String methodType ) { receivedMessage = null; final String urlValue = url; try { // Create an instance of HttpClient. HttpClient client = new SystemDefaultHttpClient(); HttpRequestBase method = null; if(methodType.equals("GET")){ method =new HttpGet(urlValue); } else{ method =new HttpPost(urlValue); } HttpResponse httpResponse = client.execute(method); receivedMessage = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); log.info(receivedMessage); } catch (Throwable t) { log.error(t); } }
public static void main(String args[]) { log.info("Starting WSO2 Http Client"); HttpUtil.setTrustStoreParams(); String url = args[0]; String username = args[1]; String password = args[2]; String sampleNumber = args[3]; String filePath = args[4]; HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); filePath = HttpUtil.getMessageFilePath(sampleNumber, filePath, url); readMsg(filePath); for (String message : messagesList) { StringEntity entity = new StringEntity(message); log.info("Sending message:"); log.info(message + "\n"); method.setEntity(entity); if (url.startsWith("https")) { processAuthentication(method, username, password); } httpClient.execute(method).getEntity().getContent().close(); } Thread.sleep(500); // Waiting time for the message to be sent } catch (Throwable t) { log.error("Error when sending the messages", t); } }
@Override public void run() { HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); log.info("Sending messages.."); long lastTime = System.currentTimeMillis(); DecimalFormat decimalFormat = new DecimalFormat("#"); while (count < noOfEvents) { count++; String temp = "{\"event\": " + getRandomEvent(count).toString() + "}"; StringEntity entity = new StringEntity(temp); method.setEntity(entity); if (url.startsWith("https")) { processAuthentication(method, username, password); } httpClient.execute(method).getEntity().getContent().close(); if (count % elapsedCount == 0) { long currentTime = System.currentTimeMillis(); long elapsedTime = currentTime - lastTime; double throughputPerSecond = (((double) elapsedCount) / elapsedTime) * 1000; lastTime = currentTime; log.info("Sent " + elapsedCount + " sensor events in " + elapsedTime + " milliseconds with total throughput of " + decimalFormat.format(throughputPerSecond) + " events per second."); } } } catch (Throwable t) { log.error("Error when sending the messages", t); } }
/** * Creates new http client by using the provided configuration. * * @param params * http client configuration, if null a client with default * configuration (no additional configuration) is created. */ public static HttpClient createClient(final SolrParams params) { final ModifiableSolrParams config = new ModifiableSolrParams(params); logger.info("Creating new http client, config:" + config); final DefaultHttpClient httpClient = new SystemDefaultHttpClient(); configureClient(httpClient, config); return httpClient; }
public HTTPRequest() { client = new SystemDefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000); }
public LayerHttpServiceImpl() { // Create a HTTP client object, which will initiate the connection: final HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT); setClient(new SystemDefaultHttpClient(httpParams)); }
public HttpClient createHttpClient() { return new SystemDefaultHttpClient(); }