List of usage examples for org.apache.http.client.methods CloseableHttpResponse close
public void close() throws IOException;
From source file:com.webarch.common.net.http.HttpService.java
/** * apache httpclient?post/*from www. jav a 2 s. c o m*/ * @param url ? * @param paramsMap ?map * @return ? */ public static String doHttpClientPost(String url, Map<String, String> paramsMap) { CloseableHttpClient client = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse response = null; try { HttpPost method = new HttpPost(url); if (paramsMap != null) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> param : paramsMap.entrySet()) { NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue()); paramList.add(pair); } method.setEntity(new UrlEncodedFormEntity(paramList, DEFAULT_CHARSET)); } response = client.execute(method); HttpEntity entity = response.getEntity(); if (entity != null) { responseText = EntityUtils.toString(entity); } } catch (Exception e) { logger.error("?HTTP?", e); } finally { try { response.close(); } catch (Exception e) { logger.error("HTTPResponse?", e); } } return responseText; }
From source file:com.nibss.util.Request.java
public void post(String url, List<NameValuePair> list) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w. j a va 2s. c o m*/ HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(list)); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine().getStatusCode()); HttpEntity entity2 = response2.getEntity(); EntityUtils.consume(entity2); } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:net.fischboeck.discogs.BaseOperations.java
private void closeSafe(CloseableHttpResponse response) { if (response != null) { try {/*ww w . j av a 2s . c om*/ response.close(); } catch (IOException ex) { // noop } } }
From source file:org.wso2.carbon.ml.analysis.test.AddModelConfigurationsTestCase.java
/** * Test adding default values to customized features an analysis. * //from ww w . j a va2 s .c om * @throws MLHttpClientException * @throws IOException */ @Test(description = "Add model configurations to the analysis") public void testSetModelConfigurations() throws MLHttpClientException, IOException { Map<String, String> configurations = new HashMap<String, String>(); configurations.put(MLConstants.ALGORITHM_NAME, "LOGISTIC_REGRESSION"); configurations.put(MLConstants.ALGORITHM_TYPE, "Classification"); configurations.put(MLConstants.RESPONSE, "Class"); configurations.put(MLConstants.TRAIN_DATA_FRACTION, "0.7"); CloseableHttpResponse response = mlHttpclient.setModelConfiguration(MLIntegrationTestConstants.ANALYSIS_ID, configurations); assertEquals("Unexpected response recieved", Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode()); response.close(); }
From source file:au.org.ncallister.goodbudget.tools.coms.GoodBudgetSession.java
public String get(String path, List<NameValuePair> parameters) throws URISyntaxException, IOException { URIBuilder builder = new URIBuilder(BASE_URI); builder.setPath(path);// w ww . ja v a 2 s.c o m builder.setParameters(parameters); HttpGet get = new HttpGet(builder.build()); CloseableHttpResponse response = client.execute(get, sessionContext); try { BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); return reader.lines().collect(Collectors.joining("\n")); } finally { response.close(); } }
From source file:com.srotya.tau.nucleus.qa.QAAlertRules.java
@Test public void testBEmailAlertRule() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException, InterruptedException { CloseableHttpClient client = null;/*from w w w . ja v a2s .co m*/ client = Utils.buildClient("http://localhost:8080/commands/templates", 2000, 2000); HttpPut templateUpload = new HttpPut("http://localhost:8080/commands/templates"); String template = AlertTemplateSerializer.serialize( new AlertTemplate((short) 1, "test_template", "alert@srotya.com", "mail", "test", "test", 30, 1), false); templateUpload.addHeader("content-type", "application/json"); templateUpload.setEntity(new StringEntity(new Gson().toJson(new TemplateCommand("all", false, template)))); CloseableHttpResponse response = client.execute(templateUpload); response.close(); assertTrue( response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300); client = Utils.buildClient("http://localhost:8080/commands/rules", 2000, 2000); HttpPut ruleUpload = new HttpPut("http://localhost:8080/commands/rules"); String rule = RuleSerializer.serializeRuleToJSONString(new SimpleRule((short) 2, "SimpleRule", true, new EqualsCondition("value", 1.0), new Action[] { new TemplatedAlertAction((short) 0, (short) 1) }), false); ruleUpload.addHeader("content-type", "application/json"); ruleUpload.setEntity(new StringEntity( new Gson().toJson(new RuleCommand(StatelessRulesEngine.ALL_RULEGROUP, false, rule)))); response = client.execute(ruleUpload); response.close(); assertTrue( response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300); client = Utils.buildClient("http://localhost:8080/events", 2000, 2000); Map<String, Object> eventHeaders = new HashMap<>(); eventHeaders.put("value", 1); eventHeaders.put("@timestamp", "2014-04-23T13:40:29.000Z"); eventHeaders.put(Constants.FIELD_EVENT_ID, 1101); HttpPost eventUpload = new HttpPost("http://localhost:8080/events"); eventUpload.addHeader("content-type", "application/json"); eventUpload.setEntity(new StringEntity(new Gson().toJson(eventHeaders))); response = client.execute(eventUpload); response.close(); assertTrue(response.getStatusLine().getReasonPhrase(), response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300); int size = 0; while ((size = AllQATests.getSmtpServer().getReceivedEmailSize()) <= 2) { System.out.println("Waiting on emails to be received:" + size); Thread.sleep(1000); } assertEquals(3, size); eventUpload = new HttpPost("http://localhost:8080/events"); eventUpload.addHeader("content-type", "application/json"); eventUpload.setEntity(new StringEntity(new Gson().toJson(eventHeaders))); response = client.execute(eventUpload); response.close(); assertTrue(response.getStatusLine().getReasonPhrase(), response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300); while ((size = AllQATests.getSmtpServer().getReceivedEmailSize()) <= 2) { System.out.println("Waiting on emails to be received:" + size); Thread.sleep(1000); } assertEquals(3, size); }
From source file:org.wso2.carbon.ml.project.test.CreateProjectsTestCase.java
/** * Test creating a project without the project name. * @throws MLHttpClientException //ww w . j ava 2 s. c o m * @throws IOException */ @Test(description = "Create a project without a dataset") public void testCreateProjectWithoutDataset() throws MLHttpClientException, IOException { CloseableHttpResponse response = mlHttpclient.createProject("TestProjectForCreatProjectTestCase-2", null); assertEquals("Unexpected response recieved", Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusLine().getStatusCode()); response.close(); }
From source file:net.oebs.jalos.Client.java
public SubmitResponseObject submit(String postUrl) throws IOException { HttpPost httpPost = new HttpPost(this.serviceUrl + "/a/submit"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("url", postUrl)); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); ObjectMapper mapper = new ObjectMapper(); SubmitResponseObject sro = mapper.readValue(entity.getContent(), SubmitResponseObject.class); EntityUtils.consume(entity);// ww w.j a va2 s. c om response.close(); return sro; }
From source file:org.wso2.carbon.ml.project.test.CreateProjectsTestCase.java
/** * Test creating a project without the project name. * @throws MLHttpClientException /*from w w w.ja v a 2 s . c o m*/ * @throws IOException */ @Test(description = "Create a project without name", dependsOnMethods = "testCreateProject") public void testCreateProjectWithoutName() throws MLHttpClientException, IOException { CloseableHttpResponse response = mlHttpclient.createProject(null, MLIntegrationTestConstants.DATASET_NAME); assertEquals("Unexpected response recieved", Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusLine().getStatusCode()); response.close(); }
From source file:com.safestream.sdk.http.SafeStreamHttpClient.java
private void tryCloseHttpResponse(CloseableHttpResponse httpResponse) throws SafeStreamHttpClientException { try {// w w w .j a v a 2 s . c o m httpResponse.close(); } catch (IOException e) { throw new SafeStreamHttpClientException(e); } }