@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); }
/** * 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"); } }
@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); } }
/** * 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; }
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(), ""); }
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; }
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()); } }
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")); } } } }
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; }
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; }
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()); } } }
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; }
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; }
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; }
@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(); } }
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; }
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; }
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; }
/** * 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; }
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(); }
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"); }
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(); } }
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; }
/** * 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; }
@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(); } */ }
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); }
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; }
@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; }
@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; }
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"); } }
/** * 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; }
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"); } }
@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(); } */ }
@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(); } */ }
@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(); }
public synchronized CloseableHttpClient getHttpClient() throws NSPException { if (null == this.httpClient) { this.httpClient = createHttpClient(); } return this.httpClient; }
/** * 获取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(); }
public void testExecuteGetWithBadResponseExceptionFromListProfilesEndpoint() throws IOException { String responseString = this.loadResourceAsString("/src/test/fixtures/internalServerErrorResponse.json"); CloseableHttpClient httpClientMock = mock(CloseableHttpClient.class); HttpGet httpGetMock = mock(HttpGet.class); CloseableHttpResponse httpResponseMock = mock(CloseableHttpResponse.class); StatusLine statusLineMock = mock(StatusLine.class); BasicHttpEntity httpEntity = new BasicHttpEntity(); InputStream stream = new ByteArrayInputStream(responseString.getBytes(StandardCharsets.UTF_8.name())); httpEntity.setContent(stream); when(statusLineMock.getStatusCode()).thenReturn(500); when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); when(httpResponseMock.getEntity()).thenReturn(httpEntity); when(httpClientMock.execute(httpGetMock)).thenReturn(httpResponseMock); Client client = new Client(httpClientMock); List<Integer> profileIds = new ArrayList<>(); profileIds.add(92); Response response = client.executeGet( new Credentials(2, "secretSanta"), new Qql( new Date(1506816000000L), new Date(), profileIds, "SELECT profileId, time, fans FROM facebook" ), httpGetMock ); try { response.getData(); } catch (Exception e) { String expectedExceptionMessage = "an internal server error has occurred, please try again later."; assertEquals(BadResponseException.class, e.getClass()); assertEquals(expectedExceptionMessage, e.getMessage()); } }
@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(); } */ }
@Override protected void configure() { bind(CEIDGClient.class).to(CEIDGClientImpl.class); bind(CookieParser.class).to(CookieParserImpl.class); bind(CaptchaLabeler.class).to(SwingCaptchaLabeler.class); bind(CaptchaDownloaderService.class).to(CaptchaDownloaderServiceImpl.class); bind(CloseableHttpClient.class).toInstance(HttpClientBuilder.create().disableCookieManagement().build()); bind(CaptchaIdParser.class).to(CaptchaIdParserImpl.class); bind(ImageParser.class).to(ImageParserImpl.class); bind(ImageWriter.class).to(ImageWriterImpl.class); bind(SessionIdParser.class).to(SessionIdParserImpl.class); bind(ImageStoreFactory.class).to(ImageStoreFactoryImpl.class); bind(RequestFactory.class).to(RequestFactoryImpl.class); bind(JsoupDocumentFactory.class).to(JsoupDocumentFactoryImpl.class); }