Java Code Examples for org.apache.http.impl.client.CloseableHttpClient#execute()

The following examples show how to use org.apache.http.impl.client.CloseableHttpClient#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpRestWb.java    From document-management-software with GNU Lesser General Public License v3.0 7 votes vote down vote up
private static String loginJSON() throws UnsupportedEncodingException, IOException {

		CloseableHttpClient httpclient = HttpClients.createDefault();

		String input = "{ \"username\" : \"admin\", \"password\" : \"admin\" }";
		System.out.println(input);

		StringEntity entity = new StringEntity(input, ContentType.create("application/json", Consts.UTF_8));
		HttpPost httppost = new HttpPost(BASE_PATH + "/services/rest/auth/login");
		httppost.setEntity(entity);

		CloseableHttpResponse response = httpclient.execute(httppost);
		try {
			HttpEntity rent = response.getEntity();
			if (rent != null) {
				String respoBody = EntityUtils.toString(rent, "UTF-8");
				System.out.println(respoBody);
				return respoBody;
			}
		} finally {
			response.close();
		}

		return null;
	}
 
Example 2
Source File: HttpUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Http get method with headers.
 *
 * @param url the url
 * @param headers the headers
 * @return the string
 * @throws Exception the exception
 */
public static String httpGetMethodWithHeaders(String url,Map<String, Object> headers) throws Exception {
    String json = null;
    
    HttpGet get = new HttpGet(url);
    CloseableHttpClient httpClient = null;
    if (headers != null && !headers.isEmpty()) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            get.setHeader(entry.getKey(), entry.getValue().toString());
        }
    }
    try {
        httpClient = getHttpClient();
        CloseableHttpResponse res = httpClient.execute(get);
        if (res.getStatusLine().getStatusCode() == 200) {
            json = EntityUtils.toString(res.getEntity());
        }
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
    return json;
}
 
Example 3
Source File: Demo.java    From java-Crawler with MIT License 6 votes vote down vote up
public static Queue getUrlQueue(String url) throws Exception{
    Queue queue = new Queue() ;
    CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url) ;
    CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpGet) ;
    HttpEntity httpEntity = closeableHttpResponse.getEntity() ;
    String index = EntityUtils.toString(httpEntity,"gb2312");

    Document doc = Jsoup.parse(index);
    Elements elements = doc.select("a");
    for(Element element : elements) {
        String aurl = element.attr("href");

        if(aurl.indexOf("webPlay")!=-1){

        }else {
            queue.enQueue("http://www.dy2018.com" + aurl);
        }
    }
    return queue ;
}
 
Example 4
Source File: ContentInBoundPipelineTest.java    From JerryMouse with MIT License 6 votes vote down vote up
@Test
public void testContextInBoundPipeline() throws Exception {
    int availablePort = NetUtils.getAvailablePort();
    HttpServer httpSever = new HttpServer("/tmp/static");
    List<Context> contexts = httpSever.getContexts();
    Context context = contexts.get(0);
    context.setPort(availablePort);
    context.init();
    context.start();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:" + availablePort + "/index.html");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    int length = EntityUtils.toByteArray(entity).length;
    Assertions.assertEquals( 994,length);
}
 
Example 5
Source File: OpenTSDBReporterTest.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testUploadFailedException(@Mocked CloseableHttpClient httpClient,
    @Capturing Logger logger) throws IOException, InterruptedException {
  new Expectations() {{
    httpClient.execute((HttpUriRequest) any);
    result = new IOException();
  }};

  MetricRegistry registry = new MetricRegistry();
  OpenTSDBReporter reporter = makeReporter();
  registry.addReporter(reporter);

  Counter counter = registry.counter("counter");
  counter.inc("tag", "value");

  Thread.sleep(3000);

  new Verifications() {{
    logger.error(anyString, withInstanceOf(Throwable.class));
  }};
}
 
Example 6
Source File: UpdaterMain.java    From BigDataPlatform with GNU General Public License v3.0 6 votes vote down vote up
public static String getString(String url) {
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            byte[] b = IOUtils.toByteArray(response.getEntity().getContent());
            String str = new String(b);
            return str;
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}
 
Example 7
Source File: HttpUtils.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
/**
 * post请求
 *
 * @param msgs
 * @param url
 * @return
 * @throws ClientProtocolException
 * @throws UnknownHostException
 * @throws IOException
 */
public static String post(Map<String, Object> msgs, String url)
        throws ClientProtocolException, UnknownHostException, IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpPost request = new HttpPost(url);
        List<NameValuePair> valuePairs = new ArrayList<NameValuePair>();
        if (null != msgs) {
            for (Entry<String, Object> entry : msgs.entrySet()) {
                if (entry.getValue() != null) {
                    valuePairs.add(new BasicNameValuePair(entry.getKey(),
                            entry.getValue().toString()));
                }
            }
        }
        request.setEntity(new UrlEncodedFormEntity(valuePairs, CHARSET_UTF_8));
        CloseableHttpResponse resp = httpClient.execute(request);
        return EntityUtils.toString(resp.getEntity(), CHARSET_UTF_8);
    } finally {
        httpClient.close();
    }
}
 
Example 8
Source File: HttpUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Http get method with headers.
 *
 * @param url the url
 * @param headers the headers
 * @return the string
 * @throws Exception the exception
 */
public static String httpGetMethodWithHeaders(String url,Map<String, Object> headers) throws Exception {
    String json = null;
    
    HttpGet get = new HttpGet(url);
    CloseableHttpClient httpClient = null;
    if (headers != null && !headers.isEmpty()) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            get.setHeader(entry.getKey(), entry.getValue().toString());
        }
    }
    try {
        httpClient = getHttpClient();
        CloseableHttpResponse res = httpClient.execute(get);
        if (res.getStatusLine().getStatusCode() == 200) {
            json = EntityUtils.toString(res.getEntity());
        }
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
    return json;
}
 
Example 9
Source File: CommonService.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
public CloseableHttpResponse getCloseResponse(HttpServletRequest req, String newUrl) throws ServletException {
    CloseableHttpResponse closeResponse;
    try {
        CloseableHttpClient client = this.generateHttpClient();
        if (req.getMethod().equals(METHOD_TYPE)) {
            HttpGet get = this.getMethod(newUrl, req);
            closeResponse = client.execute(get);
        } else {
            HttpPost postMethod = this.postMethod(newUrl, req);
            closeResponse = client.execute(postMethod);
        }
    } catch (Exception e) {
        log.error("getCloseResponse fail,error:{}", e.getMessage());
        throw new ServletException(e.getMessage());
    }
    return closeResponse;
}
 
Example 10
Source File: CloudfrontAuthorizedHTMLContentDistributionRule.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public boolean isWebSiteHosted(String url) throws Exception {
	HttpGet httpGet = new HttpGet(url);
	httpGet.addHeader("content-type", "text/html");
	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
	if (httpClient != null) {
		HttpResponse httpResponse;
		try {
			httpResponse = httpClient.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() >= 400) {
				return false;
			}
		} catch (Exception e) {
			logger.error("Exception getting from url  :[{}],[{}] ", url, e.getMessage());
			throw e;
		}
	}
	return true;
}
 
Example 11
Source File: PhotosLibraryUploadCallable.java    From java-photoslibrary with Apache License 2.0 5 votes vote down vote up
/** Uploads the next byte chunk of the media item. */
private HttpResponse uploadNextChunk(String uploadUrl, long receivedByteCount)
    throws IOException {
  // Reads data from input stream
  byte[] dataChunk = new byte[optimalChunkSize];
  int readByteCount = request.readData(dataChunk);

  // Avoid uploading the whole chunk when only a part of it contains data
  if (readByteCount < optimalChunkSize) {
    dataChunk = trimByteArray(dataChunk, readByteCount);
  }

  HttpPost httpPost = createAuthenticatedPostRequest(uploadUrl);
  httpPost.addHeader(UPLOAD_PROTOCOL_HEADER, UPLOAD_PROTOCOL_VALUE);

  if (receivedByteCount + readByteCount == request.getFileSize()) {
    httpPost.addHeader(
        UPLOAD_COMMAND_HEADER, String.join(",", UploadCommands.UPLOAD, UploadCommands.FINALIZE));
  } else {
    httpPost.addHeader(UPLOAD_COMMAND_HEADER, UploadCommands.UPLOAD);
  }

  httpPost.addHeader(UPLOAD_OFFSET_HEADER, String.valueOf(receivedByteCount));
  httpPost.addHeader(UPLOAD_SIZE_HEADER, String.valueOf(readByteCount));
  httpPost.setEntity(EntityBuilder.create().setBinary(dataChunk).build());

  CloseableHttpClient httpClient =
      HttpClientBuilder.create()
          .useSystemProperties()
          .setDefaultRequestConfig(getRequestConfig())
          .build();
  return httpClient.execute(httpPost);
}
 
Example 12
Source File: HttpUtil.java    From learnjavabug with MIT License 5 votes vote down vote up
public static String post(String url, String payload) throws UnsupportedEncodingException {
    HttpPost httpPost = new HttpPost(url);
//    httpPost.addHeader("Cookie", "rememberMe=" + Base64.getEncoder().encodeToString(data));
    HttpEntity httpEntity = new StringEntity(payload, "application/x-www-form-urlencoded", "utf-8");
    httpPost.setEntity(httpEntity);
    try {
      HttpClientBuilder httpClientBuilder = HttpClients
          .custom()
//          .setProxy(new HttpHost("127.0.0.1", 8080))
          .disableRedirectHandling()
//          .disableCookieManagement()
          ;
      if (url.startsWith("https://")) {
        httpClientBuilder.setSSLSocketFactory(sslsf);
      }

      CloseableHttpClient httpClient = null;
      CloseableHttpResponse response = null;
      try {
        httpClient = httpClientBuilder.build();
        response = httpClient.execute(httpPost);
        int status = response.getStatusLine().getStatusCode();
        if (status == 200) {
          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
          StringBuilder stringBuilder = new StringBuilder();
          String line;
          while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
          }
          return stringBuilder.toString();
        }
      } finally {
        response.close();
        httpClient.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
 
Example 13
Source File: HttpServerTest.java    From JerryMouse with MIT License 5 votes vote down vote up
@Test
public void httpServerCanHandleServletWhichNotLoadOnBootStrap() throws Exception {
    int availablePort = NetUtils.getAvailablePort();
    HttpServer httpSever = new HttpServer("/tmp/static");
    List<Context> contexts = httpSever.getContexts();
    Context context = contexts.get(0);
    context.setPort(availablePort);
    httpSever.start();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:" + availablePort + "/hello");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String responseStr = EntityUtils.toString(entity);
    assertTrue(responseStr.contains("hello"));
}
 
Example 14
Source File: TokenServiceHttpsJwt.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private ClientWithResponse loginWithCredentials(String userId, String password) throws Exception {
    CloseableHttpClient client = httpsClientProvider.getHttpsClientWithTrustStore();
    HttpPost httpPost = new HttpPost(loginEndpoint);
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(new Credentials(userId, password));
    StringEntity entity = new StringEntity(json);
    httpPost.setEntity(entity);
    httpPost.setHeader("Content-type", "application/json");
    return new ClientWithResponse(client, client.execute(httpPost));
}
 
Example 15
Source File: GetProblem.java    From java-Crawler with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    CloseableHttpClient closeableHttpClient = HttpClients.createDefault() ;
    HttpPost httpPost = new HttpPost("https://www.zhihu.com/login/phone_num") ;
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("_xsrf", "66653239623962342d396237632d346233332d396331362d333434386438326438616139"));
    nvps.add(new BasicNameValuePair("password", "xxxxxxxxx"));
    nvps.add(new BasicNameValuePair("captcha_type", "cn"));
    nvps.add(new BasicNameValuePair("phone_num", "xxxxxxxxx"));

    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost) ;
    HttpEntity entity = closeableHttpResponse.getEntity() ;
    String s = EntityUtils.toString(entity);
    System.out.println(s);
}
 
Example 16
Source File: TopicService.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private <T> BaseResponse<T> invokeBrokerCGI(HttpServletRequest request, String url, TypeReference<BaseResponse<T>> typeReference) throws GovernanceException {
    CloseableHttpClient httpClient = commonService.generateHttpClient();
    HttpGet get = commonService.getMethod(url, request);

    if (StringUtils.isBlank(url)) {
        log.error("invokeBrokerCGI failed, request url is null");
        throw new GovernanceException("request url is null");
    }

    long requestStartTime = System.currentTimeMillis();
    try (CloseableHttpResponse httpResponse = httpClient.execute(get)) {
        log.info("invokeBrokerCGI {} in {} millisecond, response:{}", url,
                System.currentTimeMillis() - requestStartTime, httpResponse.getStatusLine().toString());
        if (HttpStatus.OK.value() != httpResponse.getStatusLine().getStatusCode()
                || null == httpResponse.getEntity()) {
            log.error("invokeBrokerCGI failed, request url:{}, msg:{}", url, httpResponse.getStatusLine().toString());
            throw new GovernanceException("invokeBrokerCGI failed");
        }

        byte[] responseResult = EntityUtils.toByteArray(httpResponse.getEntity());
        BaseResponse<T> baseResponse = JsonHelper.json2Object(responseResult, typeReference);

        if (ErrorCode.SUCCESS.getCode() != baseResponse.getCode()) {
            log.error("invokeBrokerCGI failed, request url:{}, msg:{}", url, baseResponse.getMessage());
            throw new GovernanceException(baseResponse.getCode(), baseResponse.getMessage());
        }

        return baseResponse;
    } catch (IOException | BrokerException e) {
        log.error("invokeBrokerCGI error, request url:{}", url, e);
        throw new GovernanceException(ErrorCode.HTTP_REQUEST_EXECUTE_ERROR);
    }
}
 
Example 17
Source File: PacHttpUtils.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Do http post.
 *
 * @param url the url
 * @param requestBody the request body
 * @param headers the headers
 * @return the string
 */
public static String getResponse(final String url, final String requestBody, final Map<String, String> headers) {
    CloseableHttpClient httpclient = null;
    if(Strings.isNullOrEmpty(url)){
        return "";
    }
    
    try {
        if (url.contains("https")) {

            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(CommonUtils.createNoSSLContext());
            httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } else {
            httpclient = HttpClients.custom().build();
        }

        HttpPost httppost = new HttpPost(url);
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httppost.addHeader(entry.getKey(), entry.getValue());
        }
        httppost.setHeader(CONTENT_TYPE, APPLICATION_JSON);
        StringEntity jsonEntity = new StringEntity(requestBody);
        httppost.setEntity(jsonEntity);
        HttpResponse httpresponse = httpclient.execute(httppost);
       if(httpresponse.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
           throw new IOException("non 200 code from rest call--->" + url);
       }
        String responseStr = EntityUtils.toString(httpresponse.getEntity());
        LOGGER.debug(url + " service with input" + requestBody +" returned " + responseStr);
        return responseStr;
    } catch (org.apache.http.ParseException parseException) {
        LOGGER.error("ParseException : " + parseException.getMessage());
    } catch (IOException ioException) {
        LOGGER.error("IOException : " + ioException.getMessage());
    }
    return null;
}
 
Example 18
Source File: HttpUtil.java    From learnjavabug with MIT License 5 votes vote down vote up
public static String get(String url) {
    HttpGet httpGet = new HttpGet(url);
    try {
      HttpClientBuilder httpClientBuilder = HttpClients
          .custom()
//          .setProxy(new HttpHost("127.0.0.1", 8080))
          .disableRedirectHandling()
//          .disableCookieManagement()
          ;
      if (url.startsWith("https://")) {
        httpClientBuilder.setSSLSocketFactory(sslsf);
      }

      CloseableHttpClient httpClient = null;
      CloseableHttpResponse response = null;
      try {
        httpClient = httpClientBuilder.build();
        response = httpClient.execute(httpGet);
        int status = response.getStatusLine().getStatusCode();
        if (status == 200) {
          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
          StringBuilder stringBuilder = new StringBuilder();
          String line;
          while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
          }
          return stringBuilder.toString();
        }
      } finally {
        response.close();
        httpClient.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
 
Example 19
Source File: HttpRestWb.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void createFolderSimpleForm(CloseableHttpClient httpclient) throws Exception { 
	
	// This will create a tree starting from the Root folder (not in the Default workspace)
	List<NameValuePair> formparams = new ArrayList<NameValuePair>();
	formparams.add(new BasicNameValuePair("folderPath", "/LogicalDOC/USA/NJ/Fair Lawn/createSimple"));
	
	UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
	
	HttpPost httppost = new HttpPost(BASE_PATH + "/services/rest/folder/createSimpleForm");
	httppost.setEntity(entity);		

	CloseableHttpResponse response = httpclient.execute(httppost);
	int code = response.getStatusLine().getStatusCode();
	System.out.println("HTTPstatus code: "+ code);
	if (code == HttpStatus.SC_OK) {
	} else {
		//log.warn("status code is invalid: {}", code);
		System.err.println("status code is invalid: "+ code);
		throw new Exception(response.getStatusLine().getReasonPhrase());
	}				
	
	try {
		HttpEntity rent = response.getEntity();
		if (rent != null) {
			String respoBody = EntityUtils.toString(rent, "UTF-8");
			System.out.println(respoBody);
		}
	} finally {
		response.close();
	}
}
 
Example 20
Source File: Qbittorrent.java    From BoxHelper with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void acquireGlobalInfo() throws IOException {
    System.out.println("\u001b[37;1m [Info]   \u001b[36m  qBittorrent:\u001b[0m Acquiring global status ...");
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://127.0.0.1:" + this.getPort() + "/api/v2/transfer/info");
    httpget.addHeader("User-Agent", "BoxHelper");
    httpget.addHeader("Host", "127.0.0.1:" + this.getPort());
    httpget.addHeader("Cookie", "SID=" + this.sessionID);
    CloseableHttpResponse response = httpClient.execute(httpget);
    String responseString = "";
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        responseString = EntityUtils.toString(entity) ;
        Gson gson = new Gson();
        QbittorrentGlobalInfo qbittorrentGlobalInfo = gson.fromJson(responseString, QbittorrentGlobalInfo.class);
        this.setDownloadSpeed(qbittorrentGlobalInfo.getDl_info_speed());
        this.setUploadSpeed(qbittorrentGlobalInfo.getUp_info_speed());
        if (this.getCount() ==1) {
            this.setAverageUp(this.getUploadSpeed());
            this.setAverageDown(this.getDownloadSpeed());
        }
        else {
            this.setAverageUp((this.getAverageUp() * (this.getCount() - 1) + this.getUploadSpeed()) / this.getCount());
            this.setAverageDown((this.getAverageDown() * (this.getCount() - 1) + this.getDownloadSpeed()) / this.getCount());
        }
        acquireTorrents(TorrentState.ALL);
        this.allTorrents.forEach(torrent -> {
            this.setTorrentSize(this.getTorrentSize() + ((QbittorrentTorrents) torrent).getTotal_size());
        });
        System.out.println("\u001b[37;1m [Info]   \u001b[36m  qBittorrent:\u001b[0m Current upload speed is \u001b[33m" +
                new DecimalFormat("#0.00").format(this.getUploadSpeed() / (double)1048576) + " MB/s\u001b[0m (Avg. " +
                new DecimalFormat("#0.00").format(this.getAverageUp() / (double)1048576) + " MB/s) .");
    } else System.out.println("\u001b[33;1m [Warning]\u001b[36m  qBittorrent:\u001b[0m Cannot acquire global status.");
    response.close();
    httpClient.close();

    checkIO();
}