Java 类org.apache.http.impl.client.CloseableHttpClient 实例源码
项目:outcomes
文件:TestHttpCore.java
@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);
}
项目:yacy_grid_mcp
文件:ClientConnection.java
/**
* 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");
}
}
项目:dcsi
文件:ForgeRockUserDao.java
@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);
}
}
项目:open-kilda
文件:Mininet.java
/**
* 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;
}
项目:devops-cstack
文件:JSONClient.java
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(), "");
}
项目:Thrush
文件:HttpRequest.java
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;
}
项目:bubble2
文件:ResponseWrap.java
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());
}
}
项目:java-web-services-training
文件:Jws1042Application.java
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"));
}
}
}
}
项目:educational-plugin
文件:EduAdaptiveStepicConnector.java
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;
}
项目:jspider
文件:HttpClientExecutor.java
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;
}
项目:smarti
文件:RocketChatEndpoint.java
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());
}
}
}
项目:alexa-gira-bridge
文件:Util.java
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;
}
项目:Thrush
文件:HttpRequest.java
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;
}
项目:dubbocloud
文件:MockTestFilter.java
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;
}
项目:Wechat-Group
文件:MaterialDeleteRequestExecutor.java
@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();
}
}
项目:Thrush
文件:HttpRequest.java
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;
}
项目:Thrush
文件:HttpRequest.java
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;
}
项目:ts-benchmark
文件:HttpPoolManager.java
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;
}
项目:mumu-core
文件:HttpClientUtil.java
/**
* 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;
}
项目:PeSanKita-android
文件:LegacyMmsConnection.java
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();
}
项目:PeSanKita-android
文件:LegacyMmsConnection.java
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");
}
项目:daelic
文件:DruidClient.java
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();
}
}
项目:devops-cstack
文件:RestUtils.java
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;
}
项目:oneops
文件:HTTPMsgService.java
/**
* 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;
}
项目:light-4-j-plugin-wrapper
文件:Data3GetHandlerTest.java
@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();
}
*/
}
项目:asch-java
文件:REST.java
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);
}
项目:Reer
文件:HttpClientHelper.java
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;
}
项目:karate
文件:ApacheHttpClient.java
@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;
}
项目:jspider
文件:DefaultHttpClientPool.java
@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;
}
项目:Java-APIs
文件:OEModelEndpoint.java
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");
}
}
项目:mumu
文件:HttpClientUtil.java
/**
* 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;
}
项目:airsonic
文件:AudioScrobblerService.java
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");
}
}
项目:light-4-j-plugin-wrapper
文件:Data22GetHandlerTest.java
@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();
}
*/
}
项目:light-4-j-plugin-wrapper
文件:Data18GetHandlerTest.java
@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();
}
*/
}
项目:educational-plugin
文件:EduStepicAuthorizedClient.java
@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();
}
项目:raven
文件:HttpConnectionAdaptor.java
public synchronized CloseableHttpClient getHttpClient() throws NSPException {
if (null == this.httpClient) {
this.httpClient = createHttpClient();
}
return this.httpClient;
}
项目:NetDiscovery
文件:HttpManager.java
/**
* 获取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();
}
项目:quintly-java-api
文件:ClientTest.java
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());
}
}
项目:light-4-j-plugin-wrapper
文件:Data8GetHandlerTest.java
@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();
}
*/
}
项目:ceidg-captcha
文件:CEIDGCaptchaDownloaderModule.java
@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);
}