org.apache.hc.core5.http.HttpEntity Java Examples

The following examples show how to use org.apache.hc.core5.http.HttpEntity. 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: TestUtil.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
public static IHttpManager returningJson(String jsonFixtureFileName) throws Exception {

      // Mocked HTTP Manager to get predictable responses
      final IHttpManager mockedHttpManager = mock(IHttpManager.class);
      String fixture = null;

      if (jsonFixtureFileName != null) {
        fixture = readTestData(jsonFixtureFileName);
      }

      when(mockedHttpManager.get(any(URI.class), any(Header[].class))).thenReturn(fixture);
      when(mockedHttpManager.post(any(URI.class), any(Header[].class), any(HttpEntity.class))).thenReturn(fixture);
      when(mockedHttpManager.put(any(URI.class), any(Header[].class), any(HttpEntity.class))).thenReturn(fixture);
      when(mockedHttpManager.delete(any(URI.class), any(Header[].class), any(HttpEntity.class))).thenReturn(fixture);

      return mockedHttpManager;
    }
 
Example #2
Source File: HttpLoaderServer.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
public void sendResponse( ClassicHttpResponse response,HttpContext context, HttpRequest request)throws HttpException, IOException
{

    response.setCode(HttpStatus.SC_OK);
    File file = new File("D:/XMLloader/testPileHttp/test/fileTestHttp.txt");
    //FileEntity body = new FileEntity(file, "text/html");
    HttpEntity body = new StringEntity("");
    response.setEntity(body);
}
 
Example #3
Source File: HttpUtils.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public static String retrieveHttpAsString(String url) throws HttpException, IOException {
    RequestConfig requestConfig = RequestConfig.custom()
            .setResponseTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectionRequestTimeout(5000, TimeUnit.MILLISECONDS)
            .setCookieSpec(StandardCookieSpec.IGNORE)
            .build();
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .setUserAgent(_userAgent)
            .build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        final int statusCode = response.getCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpException("Error " + statusCode + " for URL " + url);
        }
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new IOException("Error for URL " + url, e);
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}
 
Example #4
Source File: HttpUtils.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public static String retrieveHttpAsString(String url) throws HttpException, IOException {
    RequestConfig requestConfig = RequestConfig.custom()
            .setResponseTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectionRequestTimeout(5000, TimeUnit.MILLISECONDS)
            .setCookieSpec(StandardCookieSpec.IGNORE)
            .build();
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .setUserAgent(_userAgent)
            .build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        final int statusCode = response.getCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpException("Error " + statusCode + " for URL " + url);
        }
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new IOException("Error for URL " + url, e);
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}
 
Example #5
Source File: IHttpManager.java    From spotify-web-api-java with MIT License 2 votes vote down vote up
/**
 * Perform an HTTP POST request to the specified URL.
 *
 * @param uri     The POST request's {@link URI}.
 * @param headers The POST request's {@link Header}s.
 * @param body    The PUT request's body as a {@link HttpEntity}.
 * @return A string containing the POST request's response body.
 * @throws IOException            In case of networking issues.
 * @throws SpotifyWebApiException The Web API returned an error further specified in this exception's root cause.
 * @throws ParseException         The response could not be parsed as a string.
 */
String post(URI uri, Header[] headers, HttpEntity body) throws
  IOException,
  SpotifyWebApiException,
  ParseException;
 
Example #6
Source File: IHttpManager.java    From spotify-web-api-java with MIT License 2 votes vote down vote up
/**
 * Perform an HTTP PUT request to the specified URL.
 *
 * @param uri     The PUT request's {@link URI}.
 * @param headers The PUT request's {@link Header}s.
 * @param body    The PUT request's body as a {@link HttpEntity}.
 * @return A string containing the PUT request's response body.
 * @throws IOException            In case of networking issues.
 * @throws SpotifyWebApiException The Web API returned an error further specified in this exception's root cause.
 * @throws ParseException         The response could not be parsed as a string.
 */
String put(URI uri, Header[] headers, HttpEntity body) throws
  IOException,
  SpotifyWebApiException,
  ParseException;
 
Example #7
Source File: IHttpManager.java    From spotify-web-api-java with MIT License 2 votes vote down vote up
/**
 * Perform an HTTP DELETE request to the specified URL.
 *
 * @param uri     The DELETE request's {@link URI}.
 * @param headers The DELETE request's {@link Header}s.
 * @param body    The DELETE request's body as a {@link HttpEntity}.
 * @return A string containing the DELETE request's response body.
 * @throws IOException            In case of networking issues.
 * @throws SpotifyWebApiException The Web API returned an error further specified in this exception's root cause.
 * @throws ParseException         The response could not be parsed as a string.
 */
String delete(URI uri, Header[] headers, HttpEntity body) throws
  IOException,
  SpotifyWebApiException,
  ParseException;