Java Code Examples for org.apache.http.impl.client.CloseableHttpClient

The following are top voted examples for showing how to use org.apache.http.impl.client.CloseableHttpClient. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: outcomes   File: TestHttpCore.java   View source code 14 votes vote down vote up
@Test
public void client() throws URISyntaxException, IOException {

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder()
            .setScheme("http")
            .setHost("www.google.com")
            .setPath("/search")
            .setParameter("q", "httpclient")
            .setParameter("btnG", "Google Search")
            .setParameter("aq", "f")
            .setParameter("oq", "")
            .build();
    HttpGet httpget = new HttpGet(uri);
    CloseableHttpResponse response = httpclient.execute(httpget);
}
 
Example 2
Project: yacy_grid_mcp   File: ClientConnection.java   View source code 13 votes vote down vote up
/**
 * get a redirect for an url: this method shall be called if it is expected that a url
 * is redirected to another url. This method then discovers the redirect.
 * @param urlstring
 * @param useAuthentication
 * @return the redirect url for the given urlstring
 * @throws IOException if the url is not redirected
 */
public static String getRedirect(String urlstring) throws IOException {
    HttpGet get = new HttpGet(urlstring);
    get.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
    get.setHeader("User-Agent", ClientIdentification.getAgent(ClientIdentification.yacyInternetCrawlerAgentName).userAgent);
    CloseableHttpClient httpClient = getClosableHttpClient();
    HttpResponse httpResponse = httpClient.execute(get);
    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        if (httpResponse.getStatusLine().getStatusCode() == 301) {
            for (Header header: httpResponse.getAllHeaders()) {
                if (header.getName().equalsIgnoreCase("location")) {
                    EntityUtils.consumeQuietly(httpEntity);
                    return header.getValue();
                }
            }
            EntityUtils.consumeQuietly(httpEntity);
            throw new IOException("redirect for  " + urlstring+ ": no location attribute found");
        } else {
            EntityUtils.consumeQuietly(httpEntity);
            throw new IOException("no redirect for  " + urlstring+ " fail: " + httpResponse.getStatusLine().getStatusCode() + ": " + httpResponse.getStatusLine().getReasonPhrase());
        }
    } else {
        throw new IOException("client connection to " + urlstring + " fail: no connection");
    }
}
 
Example 3
Project: Thrush   File: HttpRequest.java   View source code 8 votes vote down vote up
public static String login(String randCode) {
    CloseableHttpClient httpClient = buildHttpClient();
    HttpPost httpPost = new HttpPost(UrlConfig.loginUrl);

    httpPost.addHeader(CookieManager.cookieHeader());
    String param = "username=" + encode(UserConfig.username) + "&password=" + encode(UserConfig.password) + "&appid=otn";
    httpPost.setEntity(new StringEntity(param, ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8)));

    String result = StringUtils.EMPTY;
    try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
        result = EntityUtils.toString(response.getEntity());
        CookieManager.touch(response);
        ResultManager.touch(result, new ResultKey("uamtk", "uamtk"));
    } catch (IOException e) {
        logger.error("login error", e);
    }

    return result;
}
 
Example 4
Project: Thrush   File: HttpRequest.java   View source code 8 votes vote down vote up
public static String checkOrderInfo(TrainQuery query) {
    CloseableHttpClient httpClient = buildHttpClient();
    HttpPost httpPost = new HttpPost(UrlConfig.checkOrderInfo);

    httpPost.addHeader(CookieManager.cookieHeader());

    httpPost.setEntity(new StringEntity(genCheckOrderInfoParam(query), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8)));

    String result = StringUtils.EMPTY;
    try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
        CookieManager.touch(response);
        result = EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        logger.error("checkUser error", e);
    }

    return result;
}
 
Example 5
Project: Thrush   File: HttpRequest.java   View source code 8 votes vote down vote up
private static byte[] captchaImage(String url) {
    Objects.requireNonNull(url);

    CloseableHttpClient httpClient = buildHttpClient();
    HttpGet httpGet = new HttpGet(url);

    httpGet.addHeader(CookieManager.cookieHeader());

    byte[] result = new byte[0];
    try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
        CookieManager.touch(response);
        result = EntityUtils.toByteArray(response.getEntity());
    } catch (IOException e) {
        logger.error("captchaImage error", e);
    }

    return result;
}
 
Example 6
Project: dcsi   File: ForgeRockUserDao.java   View source code 7 votes vote down vote up
@Override
public List<User> load() {
	try (CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
		ArrayList<User> users = new ArrayList<>();
		String url = protocol+"://" + hostAndPort + "/openidm/managed/user?_prettyPrint=true&_queryId=query-all";
		HttpGet request = new HttpGet(url);
		configure(request);
		try (CloseableHttpResponse response = httpClient.execute(request);) {
			String result = EntityUtils.toString(response.getEntity());
			logger.trace(result);
			// parse the json result and find the largest id
			JSONObject obj = new JSONObject(result);
			JSONArray arr = obj.getJSONArray("result");
			for (int i = 0; i < arr.length(); i++) {
				String usernameFromId = arr.getJSONObject(i).getString("_id");
				String email = arr.getJSONObject(i).getString("mail");
				String username = arr.getJSONObject(i).getString("userName");
				String firstname = arr.getJSONObject(i).getString("givenName");
				String lastname = arr.getJSONObject(i).getString("sn");
				User user = new User(username, email, firstname, lastname);
				users.add(user);
				if (!user.idFromUsenameForForgeRock().equals(usernameFromId)) {
					logger.warn(
							"We modeled a single field for both id and username and the server has two different values ["
									+ usernameFromId + "] != [" + username
									+ "]. The first value will be used for both.");
				}
			}
		}
		return users;
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 7
Project: open-kilda   File: Mininet.java   View source code 7 votes vote down vote up
/**
 * Simple Http Get.
 *
 * @param path the path
 * @return the CloseableHttpResponse
 * @throws URISyntaxException the URI syntax exception
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws MininetException the MininetException
 */
public CloseableHttpResponse simpleGet(String path) 
    throws URISyntaxException, IOException, MininetException {
  URI uri = new URIBuilder()
      .setScheme("http")
      .setHost(mininetServerIP.toString())
      .setPort(mininetServerPort.getPort())
      .setPath(path)
      .build();
  CloseableHttpClient client = HttpClientBuilder.create().build();
  RequestConfig config = RequestConfig
      .custom()
      .setConnectTimeout(CONNECTION_TIMEOUT_MS)
      .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
      .setSocketTimeout(CONNECTION_TIMEOUT_MS)
      .build();
  HttpGet request = new HttpGet(uri);
  request.setConfig(config);
  request.addHeader("Content-Type", "application/json");
  CloseableHttpResponse response = client.execute(request);
  if (response.getStatusLine().getStatusCode() >= 300) {
    throw new MininetException(String.format("failure - received a %d for %s.", 
        response.getStatusLine().getStatusCode(), request.getURI().toString()));
  }
  return response;
}
 
Example 8
Project: devops-cstack   File: JSONClient.java   View source code 7 votes vote down vote up
public DockerResponse sendDelete(URI uri, Boolean httpRequired) throws JSONClientException {

        if (logger.isDebugEnabled()) {
            logger.debug("Send a delete request to : " + uri);
        }
        CloseableHttpResponse response = null;
        try {
            CloseableHttpClient httpClient = buildSecureHttpClient();
            HttpDelete httpDelete = new HttpDelete(uri);
            response = httpClient.execute(httpDelete);
        } catch (IOException e) {
            throw new JSONClientException("Error in sendDelete method due to : " + e.getMessage(), e);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Status code : " + response.getStatusLine().getStatusCode());
        }

        return new DockerResponse(response.getStatusLine().getStatusCode(), "");
    }
 
Example 9
Project: bubble2   File: ResponseWrap.java   View source code 7 votes vote down vote up
public ResponseWrap(CloseableHttpClient httpClient, HttpRequestBase request, CloseableHttpResponse response, HttpClientContext context,
		ObjectMapper _mapper) {
	this.response = response;
	this.httpClient = httpClient;
	this.request = request;
	this.context = context;
	mapper = _mapper;

	try {
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			this.entity = new BufferedHttpEntity(entity);
		} else {
			this.entity = new BasicHttpEntity();
		}

		EntityUtils.consumeQuietly(entity);
		this.response.close();
	} catch (IOException e) {
		logger.warn(e.getMessage());
	}
}
 
Example 10
Project: java-web-services-training   File: Jws1042Application.java   View source code 7 votes vote down vote up
public static void main(String[] args) throws IOException, URISyntaxException {
	ObjectMapper mapper = new ObjectMapper();
	try (CloseableHttpClient client =
				 HttpClientBuilder.create().useSystemProperties().build()) {
		URI uri = new URIBuilder("http://api.geonames.org/searchJSON")
				.addParameter("q", "kabupaten garut")
				.addParameter("username", "ceefour")
				.build();
		HttpGet getRequest = new HttpGet(uri);
		try (CloseableHttpResponse resp = client.execute(getRequest)) {
			String body = IOUtils.toString(resp.getEntity().getContent(),
					StandardCharsets.UTF_8);
			JsonNode bodyNode = mapper.readTree(body);
			LOG.info("Status: {}", resp.getStatusLine());
			LOG.info("Headers: {}", resp.getAllHeaders());
			LOG.info("Body: {}", body);
			LOG.info("Body (JsonNode): {}", bodyNode);
			for (JsonNode child : bodyNode.get("geonames")) {
				LOG.info("Place: {} ({}, {})", child.get("toponymName"), child.get("lat"), child.get("lng"));
			}
		}
	}
}
 
Example 11
Project: mumu-core   File: HttpClientUtil.java   View source code 7 votes vote down vote up
/**
 * httpClient post 获取资源
 * @param url
 * @param params
 * @return
 */
public static String post(String url, Map<String, Object> params) {
	log.info(url);
	try {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpPost httpPost = new HttpPost(url);
		if (params != null && params.size() > 0) {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			Set<String> keySet = params.keySet();
			for (String key : keySet) {
				Object object = params.get(key);
				nvps.add(new BasicNameValuePair(key, object==null?null:object.toString()));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
		}
		CloseableHttpResponse response = httpClient.execute(httpPost);
		return EntityUtils.toString(response.getEntity(), "UTF-8");
	} catch (Exception e) {
		log.error(e);
	}
	return null;
}
 
Example 12
Project: PeSanKita-android   File: LegacyMmsConnection.java   View source code 7 votes vote down vote up
protected CloseableHttpClient constructHttpClient() throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  URL                 mmsc          = new URL(apn.getMmsc());
  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  if (apn.hasAuthentication()) {
    credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                                 new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
  }

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT))
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
}
 
Example 13
Project: daelic   File: DruidClient.java   View source code 7 votes vote down vote up
private <REQ, RESP> RESP query(REQ request, Class<RESP> clazz) throws IOException, DruidException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(configuration.getUrl());

    StringEntity entity = new StringEntity(mapper.writeValueAsString(request), configuration.getCharset());
    httpPost.setEntity(entity);
    httpPost.setHeader("Content-type", "application/json");

    CloseableHttpResponse response = client.execute(httpPost);
    String content = EntityUtils.toString(response.getEntity(), configuration.getCharset());

    try {
        return mapper.readValue(content, clazz);
    } catch (Exception ex) {
        throw new DruidException(content);
    } finally {
        client.close();
    }
}
 
Example 14
Project: educational-plugin   File: EduAdaptiveStepicConnector.java   View source code 6 votes vote down vote up
private static int getAttemptId(@NotNull Task task) throws IOException {
  final StepicWrappers.AdaptiveAttemptWrapper attemptWrapper = new StepicWrappers.AdaptiveAttemptWrapper(task.getStepId());

  final HttpPost post = new HttpPost(EduStepicNames.STEPIC_API_URL + EduStepicNames.ATTEMPTS);
  post.setEntity(new StringEntity(new Gson().toJson(attemptWrapper)));

  final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient();
  if (client == null) return -1;
  setTimeout(post);
  final CloseableHttpResponse httpResponse = client.execute(post);
  final int statusCode = httpResponse.getStatusLine().getStatusCode();
  final HttpEntity entity = httpResponse.getEntity();
  final String entityString = EntityUtils.toString(entity);
  EntityUtils.consume(entity);
  if (statusCode == HttpStatus.SC_CREATED) {
    final StepicWrappers.AttemptContainer container =
      new Gson().fromJson(entityString, StepicWrappers.AttemptContainer.class);
    return (container.attempts != null && !container.attempts.isEmpty()) ? container.attempts.get(0).id : -1;
  }
  return -1;
}
 
Example 15
Project: jspider   File: HttpClientExecutor.java   View source code 6 votes vote down vote up
public <T> Response<T> execute() {
    HttpProxy httpProxy = getHttpProxyFromPool();
    CookieStore cookieStore = getCookieStoreFromPool();

    CloseableHttpClient httpClient = httpClientPool.getHttpClient(siteConfig, request);
    HttpUriRequest httpRequest = httpClientPool.createHttpUriRequest(siteConfig, request, createHttpHost(httpProxy));
    CloseableHttpResponse httpResponse = null;
    IOException executeException = null;
    try {
        HttpContext httpContext = createHttpContext(httpProxy, cookieStore);
        httpResponse = httpClient.execute(httpRequest, httpContext);
    } catch (IOException e) {
        executeException = e;
    }

    Response<T> response = ResponseFactory.createResponse(
            request.getResponseType(), siteConfig.getCharset(request.getUrl()));

    response.handleHttpResponse(httpResponse, executeException);

    return response;
}
 
Example 16
Project: alexa-gira-bridge   File: Util.java   View source code 6 votes vote down vote up
public static String triggerHttpGetWithCustomSSL(String requestUrl) {
	String result = null;
	try {
		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadTrustMaterial(null, new TrustSelfSignedStrategy() {
			public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
				return true;
			}
		});
		@SuppressWarnings("deprecation")
		HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), hostnameVerifier);

		CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

		HttpGet httpGet = new HttpGet("https://" + requestUrl);
		CloseableHttpResponse response = httpclient.execute(httpGet);
		try {
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				result = EntityUtils.toString(entity);
				log.debug("Received response: " + result);
			} else {
				log.error("Request not successful. StatusCode was " + response.getStatusLine().getStatusCode());
			}
		} finally {
			response.close();
		}
	} catch (IOException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
		log.error("Error executing the request.", e);
	}
	return result;
}
 
Example 17
Project: dubbocloud   File: MockTestFilter.java   View source code 6 votes vote down vote up
private String post(String url, List<NameValuePair> nvps) throws IOException{
	CloseableHttpClient httpclient = connectionPoolManage.getHttpClient();
	
	HttpPost httpPost = new HttpPost(url);
	
	if(nvps != null)
		httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
	
	CloseableHttpResponse response = httpclient.execute(httpPost);
	
	String result = null;
	if(response.getStatusLine().getStatusCode() == 200){
		HttpEntity entity = response.getEntity();
		result = EntityUtils.toString(entity);
	}
	
	httpclient.close();
	
	return result;
}
 
Example 18
Project: Wechat-Group   File: MaterialDeleteRequestExecutor.java   View source code 6 votes vote down vote up
@Override
public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
  HttpPost httpPost = new HttpPost(uri);
  if (httpProxy != null) {
    RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
    httpPost.setConfig(config);
  }

  Map<String, String> params = new HashMap<>();
  params.put("media_id", materialId);
  httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
  try(CloseableHttpResponse response = httpclient.execute(httpPost)){
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
      throw new WxErrorException(error);
    } else {
      return true;
    }
  }finally {
    httpPost.releaseConnection();
  }
}
 
Example 19
Project: Thrush   File: HttpRequest.java   View source code 6 votes vote down vote up
public static String ticketQuery(TrainQuery trainQuery) {
    Objects.requireNonNull(trainQuery);

    CloseableHttpClient httpClient = buildHttpClient();
    HttpGet httpGet = new HttpGet(UrlConfig.ticketQuery + "?" + genQueryParam(trainQuery));

    httpGet.setHeader(CookieManager.cookieHeader());

    String result = StringUtils.EMPTY;
    try(CloseableHttpResponse response = httpClient.execute(httpGet)) {
        CookieManager.touch(response);
        result = EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        logger.error("ticketQuery error", e);
    }

    return result;
}
 
Example 20
Project: ts-benchmark   File: HttpPoolManager.java   View source code 6 votes vote down vote up
public static CloseableHttpClient  getHttpClient() {       
//        CloseableHttpClient httpClient = HttpClients.custom()
//                .setConnectionManager(cm)
//                .build();          
//        return httpClient;
    	if(httpClient==null){
    		synchronized (HttpPoolManager.class) {
    			if(httpClient==null){
//    				ConnectionConfig config = ConnectionConfig.custom()
//    		                .setBufferSize(4128)
//    		                .build();
    				httpClient = HttpClients.custom()
    						.setConnectionManager(cm)
//    						.setDefaultConnectionConfig(config)
    						.build();       
    			}
			}
    	}
    	return httpClient;
    }
 
Example 21
Project: PeSanKita-android   File: LegacyMmsConnection.java   View source code 6 votes vote down vote up
protected byte[] execute(HttpUriRequest request) throws IOException {
  Log.w(TAG, "connecting to " + apn.getMmsc());

  CloseableHttpClient   client   = null;
  CloseableHttpResponse response = null;
  try {
    client   = constructHttpClient();
    response = client.execute(request);

    Log.w(TAG, "* response code: " + response.getStatusLine());

    if (response.getStatusLine().getStatusCode() == 200) {
      return parseResponse(response.getEntity().getContent());
    }
  } catch (NullPointerException npe) {
    // TODO determine root cause
    // see: https://github.com/WhisperSystems/Signal-Android/issues/4379
    throw new IOException(npe);
  } finally {
    if (response != null) response.close();
    if (client != null)   client.close();
  }

  throw new IOException("unhandled response code");
}
 
Example 22
Project: uberscriptquery   File: HttpUtils.java   View source code 6 votes vote down vote up
public static HttpResponse get(String url, Map<String, String> header) {
    int statusCode = 0;
    String responseBody = "";

    try {
        logger.info(String.format("Getting url: %s", url));
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            if (header != null && !header.isEmpty()) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpGet.addHeader(entry.getKey(), entry.getValue());
                }
            }
            try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()))) {
                    responseBody = IOUtils.toString(reader);
                    logger.info(String.format("Finished getting url: %s, Http status: %s, Response body: %s",
                            url, statusCode, truncateLogString(responseBody)));
                }
            }
        }
        return new HttpResponse(statusCode, responseBody);
    } catch (Exception ex) {
        String msg = String.format("Failed getting url: %s", url);
        logger.warn(msg, ex);
        throw new RuntimeException(msg, ex);
    }
}
 
Example 23
Project: devops-cstack   File: RestUtils.java   View source code 6 votes vote down vote up
public Map<String, String> sendGetFileCommand(String url, String filePath, Map<String, Object> parameters)
        throws ManagerResponseException {
    Map<String, String> response = new HashMap<String, String>();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(url);
    try {
        CloseableHttpResponse httpResponse = httpclient.execute(httpget, localContext);
        InputStream inputStream = httpResponse.getEntity().getContent();
        FileOutputStream fos = new FileOutputStream(new File(filePath));
        int inByte;
        while ((inByte = inputStream.read()) != -1)
            fos.write(inByte);
        inputStream.close();
        fos.close();
        httpResponse.close();

    } catch (Exception e) {
        throw new ManagerResponseException(e.getMessage(), e);
    }

    return response;
}
 
Example 24
Project: devops-cstack   File: JSONClient.java   View source code 6 votes vote down vote up
public DockerResponse sendGet(URI uri) throws JSONClientException {

        if (logger.isDebugEnabled()) {
            logger.debug("Send a get request to : " + uri);
        }
        StringBuilder builder = new StringBuilder();


        HttpGet httpGet = new HttpGet(uri);
        HttpResponse response = null;
        try {
            CloseableHttpClient httpclient = buildSecureHttpClient();
            response = httpclient.execute(httpGet);
            LineIterator iterator = IOUtils.lineIterator(response.getEntity()
                    .getContent(), "UTF-8");
            while (iterator.hasNext()) {
                builder.append(iterator.nextLine());
            }
        } catch (IOException e) {
            throw new JSONClientException("Error in sendGet method due to : " + e.getMessage(), e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Status code : " + response.getStatusLine().getStatusCode());
            logger.debug("Server response : " + builder.toString());
        }

        return new DockerResponse(response.getStatusLine().getStatusCode(), builder.toString());

    }
 
Example 25
Project: oneops   File: HTTPMsgService.java   View source code 5 votes vote down vote up
/**
 * Posts the message to http endpoint
 *
 * @param msg the notification message
 * @param sub URL subscriber
 * @return <code>true</code> if response code is 200, else return <code>false</code>
 */
@Override
public boolean postMessage(NotificationMessage msg, BasicSubscriber sub) {
    URLSubscriber urlSub = (URLSubscriber) sub;
    boolean isHpom = urlSub.hasHpomXfmr();

    CloseableHttpClient httpClient = HttpClients.createDefault();

    HttpPost req = new HttpPost(urlSub.getUrl());
    req.setEntity(new StringEntity(gson.toJson(msg), ContentType.APPLICATION_JSON));

    int timeout = urlSub.getTimeout();
    req.setConfig(RequestConfig.custom().setSocketTimeout(timeout > 0 ? timeout : 2000).build());
    String userName = urlSub.getUserName();
    if (userName != null && StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(urlSub.getPassword()) ) {
        String auth = userName + ":" + urlSub.getPassword();
        req.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encodeBase64(auth.getBytes())));
    }

    try (CloseableHttpResponse res = httpClient.execute(req)) {
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            countOK(isHpom);
            return true;
        } else {
            logger.warn(isHpom ? "HPOM" : "HTTP" + " message post response code: "
                    + res.getStatusLine().getStatusCode()
                    + " for URL sink: "
                    + urlSub.getName());
        }
    } catch (IOException ex) {
        logger.error(isHpom ? "HPOM" : "HTTP" + " message post failed." + ex.getMessage());
    }

    countErr(isHpom);
    return false;
}
 
Example 26
Project: light-4-j-plugin-wrapper   File: Data3GetHandlerTest.java   View source code 5 votes vote down vote up
@Test
public void testData3GetHandler() throws ClientException, ApiException {
    CloseableHttpClient client = Client.getInstance().getSyncClient();
    HttpGet httpGet = new HttpGet("http://localhost:8080/apia/data3");
    /*
    Client.getInstance().addAuthorization(httpPost);
    try {
        CloseableHttpResponse response = client.execute(httpGet);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("listData", IOUtils.toString(response.getEntity().getContent(), "utf8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    */
}
 
Example 27
Project: asch-java   File: REST.java   View source code 5 votes vote down vote up
protected static HttpResponse rawPost(String url, String requestBody, Map<String,String> customeHeads, String charset ) throws IOException{
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost(encodeUrl(url));

    addCustomeHeads(customeHeads, post);
    StringEntity entity = createEntity(requestBody, charset);
    post.setEntity(entity);

    logger.debug(String.format("POST url:%s, body:%s", url, requestBody));
    return httpClient.execute(post);
}
 
Example 28
Project: Reer   File: HttpClientHelper.java   View source code 5 votes vote down vote up
private synchronized CloseableHttpClient getClient() {
    if (client == null) {
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setRedirectStrategy(new AlwaysRedirectRedirectStrategy());
        new HttpClientConfigurer(settings).configure(builder);
        this.client = builder.build();
    }
    return client;
}
 
Example 29
Project: karate   File: ApacheHttpClient.java   View source code 5 votes vote down vote up
@Override
protected HttpResponse makeHttpRequest(HttpEntity entity, long startTime) {
    if (entity != null) {
        requestBuilder.setEntity(entity);
        requestBuilder.setHeader(entity.getContentType());
    }
    HttpUriRequest httpRequest = requestBuilder.build();
    CloseableHttpClient client = clientBuilder.build();
    BasicHttpContext context = new BasicHttpContext();
    context.setAttribute(URI_CONTEXT_KEY, getRequestUri());
    CloseableHttpResponse httpResponse;
    byte[] bytes;
    try {
        httpResponse = client.execute(httpRequest, context);
        HttpEntity responseEntity = httpResponse.getEntity();
        if (responseEntity == null || responseEntity.getContent() == null) {
            bytes = new byte[0];
        } else {
            InputStream is = responseEntity.getContent();
            bytes = FileUtils.toBytes(is);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    long responseTime = getResponseTime(startTime);
    HttpResponse response = new HttpResponse(responseTime);
    response.setUri(getRequestUri());
    response.setBody(bytes);
    response.setStatus(httpResponse.getStatusLine().getStatusCode());
    for (Cookie c : cookieStore.getCookies()) {
        com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
        cookie.put(DOMAIN, c.getDomain());
        cookie.put(PATH, c.getPath());
        if (c.getExpiryDate() != null) {
            cookie.put(EXPIRES, c.getExpiryDate().getTime() + "");
        }
        cookie.put(PERSISTENT, c.isPersistent() + "");
        cookie.put(SECURE, c.isSecure() + "");
        response.addCookie(cookie);
    }
    cookieStore.clear(); // we rely on the StepDefs for cookie 'persistence'
    for (Header header : httpResponse.getAllHeaders()) {
        response.addHeader(header.getName(), header.getValue());
    }
    return response;
}
 
Example 30
Project: jspider   File: DefaultHttpClientPool.java   View source code 5 votes vote down vote up
@Override
public CloseableHttpClient getHttpClient(SiteConfig siteConfig, Request request) {
    String host = getHttpClientCacheKey(siteConfig, request);
    CloseableHttpClient httpClient = httpClients.get(host);
    if (httpClient == null) {
        synchronized (this) {
            httpClient = httpClients.get(host);
            if (httpClient == null) {
                httpClient = factory.createHttpClient(siteConfig);
                httpClients.put(host, httpClient);
            }
        }
    }
    return httpClient;
}
 
Example 31
Project: Java-APIs   File: OEModelEndpoint.java   View source code 5 votes vote down vote up
public String fetchData() throws IOException, OEConnectionException {
  String fetchUri = buildOEExportApiUrl();

  HttpClientBuilder clientBuilder = HttpClientBuilder.create();
  setCloudAuthHeaderIfConfigured(clientBuilder);
  try (CloseableHttpClient httpClient = clientBuilder.build()) {
    HttpGet httpGet = new HttpGet(fetchUri);

    HttpResponse response = httpClient.execute(httpGet);
    if (response == null) throw new OEConnectionException("Null response from http client: " + fetchUri);
    if (response.getStatusLine() == null)
      throw new OEConnectionException("Null status line from http client: " + fetchUri);


    int statusCode = response.getStatusLine().getStatusCode();

    if (logger.isDebugEnabled())
      logger.debug("HTTP request complete: " + statusCode + " " + fetchUri);

    if (statusCode != HttpStatus.SC_OK) {
      throw new OEConnectionException("Status code " + statusCode + " received from URL: " + fetchUri);
    }

    HttpEntity entity = response.getEntity();

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    entity.writeTo(byteArrayOutputStream);
    return new String(byteArrayOutputStream.toByteArray(), "UTF-8");
  }
}
 
Example 32
Project: mumu   File: HttpClientUtil.java   View source code 5 votes vote down vote up
/**
 * httpClient delete 删除资源
 * @param url
 * @return
 */
public static String delete(String url) {
	log.info(url);
	try {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpDelete httpDelete = new HttpDelete(url);
		CloseableHttpResponse response = httpClient.execute(httpDelete);
		return EntityUtils.toString(response.getEntity(), "UTF-8");
	} catch (Exception e) {
		log.error(e);
	}
	return null;
}
 
Example 33
Project: airsonic   File: AudioScrobblerService.java   View source code 5 votes vote down vote up
private String[] executeRequest(HttpUriRequest request) throws IOException {

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response = client.execute(request, responseHandler);
            return response.split("\\n");

        }
    }
 
Example 34
Project: light-4-j-plugin-wrapper   File: Data22GetHandlerTest.java   View source code 5 votes vote down vote up
@Test
public void testData22GetHandler() throws ClientException, ApiException {
    CloseableHttpClient client = Client.getInstance().getSyncClient();
    HttpGet httpGet = new HttpGet("http://localhost:8080/apic/data22");
    /*
    Client.getInstance().addAuthorization(httpPost);
    try {
        CloseableHttpResponse response = client.execute(httpGet);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("listData", IOUtils.toString(response.getEntity().getContent(), "utf8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    */
}
 
Example 35
Project: smarti   File: RocketChatEndpoint.java   View source code 5 votes vote down vote up
private void notifyRocketChat(String callbackUrl, Conversation conversation, String token) {
    if (StringUtils.isBlank(callbackUrl)) return;

    try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
        final HttpPost post = new HttpPost(callbackUrl);
        final MultiValueMap<String, String> props = CollectionUtils.toMultiValueMap(conversation.getMeta().getProperties());
        post.setEntity(new StringEntity(
                toJsonString(new SmartiUpdatePing(conversation.getId(), props.getFirst(ConversationMeta.PROP_CHANNEL_ID), token)),
                ContentType.APPLICATION_JSON
        ));
        httpClient.execute(post, response -> null);
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.error("Callback to Rocket.Chat <{}> failed: {}", callbackUrl, e.getMessage(), e);
        } else {
            log.error("Callback to Rocket.Chat <{}> failed: {}", callbackUrl, e.getMessage());
        }
    }
}
 
Example 36
Project: light-4-j-plugin-wrapper   File: Data18GetHandlerTest.java   View source code 5 votes vote down vote up
@Test
public void testData18GetHandler() throws ClientException, ApiException {
    CloseableHttpClient client = Client.getInstance().getSyncClient();
    HttpGet httpGet = new HttpGet("http://localhost:8080/apid/data18");
    /*
    Client.getInstance().addAuthorization(httpPost);
    try {
        CloseableHttpResponse response = client.execute(httpGet);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("listData", IOUtils.toString(response.getEntity().getContent(), "utf8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    */
}
 
Example 37
Project: educational-plugin   File: EduStepicAuthorizedClient.java   View source code 5 votes vote down vote up
@NotNull
private static CloseableHttpClient createInitializedClient(@NotNull String accessToken) {
  final List<BasicHeader> headers = new ArrayList<>();
  headers.add(new BasicHeader("Authorization", "Bearer " + accessToken));
  headers.add(new BasicHeader("Content-type", EduStepicNames.CONTENT_TYPE_APP_JSON));
  return getBuilder().setDefaultHeaders(headers).build();
}
 
Example 38
Project: raven   File: HttpConnectionAdaptor.java   View source code 5 votes vote down vote up
public synchronized CloseableHttpClient getHttpClient() throws NSPException {
	if (null == this.httpClient) {
		this.httpClient = createHttpClient();
	}

	return this.httpClient;
}
 
Example 39
Project: NetDiscovery   File: HttpManager.java   View source code 5 votes vote down vote up
/**
 * 获取Http客户端连接对象
 * @param timeOut 超时时间
 * @param proxy   代理
 * @param cookie  Cookie
 * @return Http客户端连接对象
 */
public CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) {

    // 创建Http请求配置参数
    RequestConfig.Builder builder = RequestConfig.custom()
            // 获取连接超时时间
            .setConnectionRequestTimeout(timeOut)
            // 请求超时时间
            .setConnectTimeout(timeOut)
            // 响应超时时间
            .setSocketTimeout(timeOut)
            .setCookieSpec(CookieSpecs.STANDARD);

    if (proxy!=null) {
        builder.setProxy(proxy);
    }

    RequestConfig requestConfig = builder.build();

    // 创建httpClient
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder
            // 把请求相关的超时信息设置到连接客户端
            .setDefaultRequestConfig(requestConfig)
            // 把请求重试设置到连接客户端
            .setRetryHandler(new RetryHandler())
            // 配置连接池管理对象
            .setConnectionManager(connManager);

    if (cookie!=null) {
        CookieStore cookieStore = new BasicCookieStore();
        cookieStore.addCookie(cookie);
        httpClientBuilder.setDefaultCookieStore(cookieStore);
    }

    return httpClientBuilder.build();
}
 
Example 40
Project: light-4-j-plugin-wrapper   File: Data8GetHandlerTest.java   View source code 5 votes vote down vote up
@Test
public void testData8GetHandler() throws ClientException, ApiException {
    CloseableHttpClient client = Client.getInstance().getSyncClient();
    HttpGet httpGet = new HttpGet("http://localhost:8080/apid/data8");
    /*
    Client.getInstance().addAuthorization(httpPost);
    try {
        CloseableHttpResponse response = client.execute(httpGet);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("listData", IOUtils.toString(response.getEntity().getContent(), "utf8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    */
}