org.apache.http.client.methods.HttpGet Java Examples

The following examples show how to use org.apache.http.client.methods.HttpGet. 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: RestClient.java    From kylin with Apache License 2.0 6 votes vote down vote up
private String getConfiguration(String url, boolean ifAuth) throws IOException {
    HttpGet request = ifAuth ? newGet(url) : new HttpGet(url);
    HttpResponse response = null;
    try {
        response = client.execute(request);
        String msg = EntityUtils.toString(response.getEntity());

        if (response.getStatusLine().getStatusCode() != 200)
            throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode()
                    + " with cache wipe url " + url + "\n" + msg);

        Map<String, String> map = JsonUtil.readValueAsMap(msg);
        msg = map.get("config");
        return msg;
    } finally {
        cleanup(request, response);
    }
}
 
Example #2
Source File: HDR.java    From goprohero with MIT License 6 votes vote down vote up
public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}
 
Example #3
Source File: CustomerCli.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void customers() throws Exception {
    String baseUrl = keycloak.getDeployment().getAuthServerBaseUrl();
    baseUrl = baseUrl.substring(0, baseUrl.indexOf('/', 8));

    String customersUrl = baseUrl + "/database/customers";
    HttpGet get = new HttpGet(customersUrl);
    get.setHeader("Accept", "application/json");
    get.setHeader("Authorization", "Bearer " + keycloak.getTokenString(10, TimeUnit.SECONDS));

    HttpResponse response = keycloak.getDeployment().getClient().execute(get);
    if (response.getStatusLine().getStatusCode() == 200) {
        print(response.getEntity().getContent());
    } else {
        System.out.println(response.getStatusLine().toString());
    }
}
 
Example #4
Source File: SettingsBasedSSLConfiguratorTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
@Test
public void testTrustAll() throws Exception {
    try (TestServer testServer = new TestServer("sslConfigurator/jks/truststore.jks",
            "sslConfigurator/jks/node1-keystore.jks", "secret", false)) {
        Path rootCaJksPath = FileHelper.getAbsoluteFilePathFromClassPath("sslConfigurator/jks/other-root-ca.jks");

        Settings settings = Settings.builder().put("prefix.enable_ssl", "true").put("prefix.trust_all", "true")
                .put("path.home", rootCaJksPath.getParent().toString()).build();
        Path configPath = rootCaJksPath.getParent();

        SettingsBasedSSLConfigurator sbsc = new SettingsBasedSSLConfigurator(settings, configPath, "prefix");

        SSLConfig sslConfig = sbsc.buildSSLConfig();

        try (CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(sslConfig.toSSLConnectionSocketFactory()).build()) {

            try (CloseableHttpResponse response = httpClient.execute(new HttpGet(testServer.getUri()))) {
                // Success
            }
        }
    }
}
 
Example #5
Source File: AutoConnectTask.java    From adb_wireless with Apache License 2.0 6 votes vote down vote up
@Override
protected Void doInBackground(Void... params)
{
	try
	{
		URI url = new URI(this.url);
		System.out.println("url = " + this.url);
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet method = new HttpGet(url);
		httpClient.execute(method);
	}
	catch (Exception e)
	{
		Debug.error("ERROR doInBackground()", e);
	}
	return null;
}
 
Example #6
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 #7
Source File: ConnectionManagerTest.java    From curly with Apache License 2.0 6 votes vote down vote up
@Test
public void getAuthenticatedConnection() throws IOException {
    webserver.requireLogin = true;
    AuthHandler handler = new AuthHandler(
            new ReadOnlyStringWrapper("localhost:"+webserver.port), 
            new ReadOnlyBooleanWrapper(false), 
            new ReadOnlyStringWrapper(TEST_USER), 
            new ReadOnlyStringWrapper(TEST_PASSWORD)
    );
    CloseableHttpClient client = handler.getAuthenticatedClient();
    assertNotNull(client);
    HttpUriRequest request = new HttpGet("http://localhost:"+webserver.port+"/testUri");
    client.execute(request);
    Header authHeader = webserver.lastRequest.getFirstHeader("Authorization");
    assertNotNull(authHeader);
    String compareToken = "Basic "+Base64.getEncoder().encodeToString((TEST_USER + ":" + TEST_PASSWORD).getBytes());
    assertEquals("Auth token should be expected format", authHeader.getValue(), compareToken);
}
 
Example #8
Source File: ClientCertTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("UT3 - P3")
public void testClientCertSuccess() throws Exception {
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(clientSSLContext);
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
    HttpResponse result = client.execute(get);
    assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());

    Header[] values = result.getHeaders("ProcessedBy");
    assertEquals("ProcessedBy Headers", 1, values.length);
    assertEquals("ResponseHandler", values[0].getValue());

    values = result.getHeaders("AuthenticatedUser");
    assertEquals("AuthenticatedUser Headers", 1, values.length);
    assertEquals("CN=Test Client,OU=OU,O=Org,L=City,ST=State,C=GB", values[0].getValue());
    HttpClientUtils.readResponse(result);
    assertSingleNotificationType(EventType.AUTHENTICATED);
}
 
Example #9
Source File: ApiGatewayTest.java    From Inside_Android_Testing with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldMakeRemoteGetCalls() {
    Robolectric.getBackgroundScheduler().pause();

    TestGetRequest apiRequest = new TestGetRequest();
    apiGateway.makeRequest(apiRequest, responseCallbacks);

    Robolectric.addPendingHttpResponse(200, GENERIC_XML);

    Robolectric.getBackgroundScheduler().runOneTask();

    HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0);
    HttpRequest sentHttpRequest = sentHttpRequestData.getHttpRequest();
    assertThat(sentHttpRequest.getRequestLine().getUri(), equalTo("www.example.com"));
    assertThat(sentHttpRequest.getRequestLine().getMethod(), equalTo(HttpGet.METHOD_NAME));

    assertThat(sentHttpRequest.getHeaders("foo")[0].getValue(), equalTo("bar"));

    CredentialsProvider credentialsProvider =
            (CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER);
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), CoreMatchers.equalTo("spongebob"));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), CoreMatchers.equalTo("squarepants"));
}
 
Example #10
Source File: JAXRSClientServerUserResourceDefaultTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void getAndCompare(String address,
                           String acceptType,
                           int expectedStatus,
                           long expectedId) throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpGet get = new HttpGet(address);
    get.setHeader("Accept", acceptType);
    try {
        CloseableHttpResponse response = client.execute(get);
        assertEquals(expectedStatus, response.getStatusLine().getStatusCode());
        Book book = readBook(response.getEntity().getContent());
        assertEquals(expectedId, book.getId());
        assertEquals("CXF in Action", book.getName());
    } finally {
        get.releaseConnection();
    }
}
 
Example #11
Source File: Mp3Converter.java    From alexa-meets-polly with Apache License 2.0 6 votes vote down vote up
public static String convertMp3(final String mp3Path) throws URISyntaxException, IOException {
    // get credentials for webservice from application config
    final String apiKey = SkillConfig.getTranslatorConvertServiceUser();
    final String apiPass = SkillConfig.getTranslatorConvertServicePass();
    // build uri
    final String bucketName = SkillConfig.getS3BucketName();
    final URIBuilder uri = new URIBuilder(SkillConfig.getTranslatorConvertServiceUrl()).addParameter("bucket", bucketName).addParameter("path", mp3Path);
    // set up web request
    final HttpGet httpGet = new HttpGet(uri.build());
    httpGet.setHeader("Content-Type", "text/plain");
    // set up credentials
    final CredentialsProvider provider = new BasicCredentialsProvider();
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(apiKey, apiPass);
    provider.setCredentials(AuthScope.ANY, credentials);
    // send request to convert webservice
    final HttpResponse response =
            HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build().execute(httpGet);

    //Validate.inclusiveBetween(200, 399, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
    // work on response
    final HttpEntity entity = response.getEntity();
    return IOUtils.toString(entity.getContent(), "UTF-8");
}
 
Example #12
Source File: JiraHttpClient.java    From benten with MIT License 6 votes vote down vote up
public JSONObject getCreateMetaData(String projectKey, String issueType){
    try{
        Map<String, String> params = new HashMap();
        params.put("expand", "projects.issuetypes.fields");
        params.put("projectKeys", projectKey);
        params.put("issuetypeNames", issueType);
        HttpGet httpGet = new HttpGet(JiraHttpHelper.metaDataUri(params));
        HttpResponse httpResponse = request(httpGet);
        if(httpResponse.getStatusLine().getStatusCode()!=200){
            handleJiraException(httpResponse);
        }
        String json = EntityUtils.toString(httpResponse.getEntity());
        JSONObject jsonObject = JiraConverter.objectMapper
                .readValue(json,JSONObject.class);
        List<Project> projects = JiraConverter.objectMapper.readValue(jsonObject.get("projects").toString(),new TypeReference<List<Project>>(){});

        if(projects.isEmpty() || projects.get(0).getIssuetypes().isEmpty()) {
            throw new BentenJiraException("Project '"+ projectKey + "'  or issue type '" + issueType +
                    "' missing from create metadata. Do you have enough permissions?");
        }
        return projects.get(0).getIssuetypes().get(0).getFields();
    }catch(Exception ex){
        throw new RuntimeException(ex);
    }
}
 
Example #13
Source File: ListedContainers.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Iterator<Container> iterator() {
    final URIBuilder uriBuilder = new UncheckedUriBuilder(
        super.baseUri().toString().concat("/json")
    );
    if (this.withSize) {
        uriBuilder.addParameter("size", "true");
    }
    final FilteredUriBuilder uri = new FilteredUriBuilder(
        uriBuilder,
        this.filters);
    
    return new ResourcesIterator<>(
        super.client(),
        new HttpGet(uri.build()),
        json -> new RtContainer(
            json,
            super.client(),
            URI.create(
                super.baseUri().toString() + "/" + json.getString("Id")
            ),
            super.docker()
        )
    );
}
 
Example #14
Source File: LongURLTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("UT3 - P3")
public void testLargeURL() throws IOException {

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < COUNT; ++i) {
        sb.append(MESSAGE);
    }
    String message = sb.toString();

    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/" + message);
        HttpResponse result = client.execute(get);
        Assert.assertEquals("/" + message, HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example #15
Source File: LotsOfQueryParametersTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test @AjpIgnore
public void testLotsOfQueryParameters_Default_Ok() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        StringBuilder qs = new StringBuilder();
        for (int i = 0; i < DEFAULT_MAX_PARAMETERS; ++i) {
            qs.append(QUERY + i);
            qs.append("=");
            qs.append(URLEncoder.encode(MESSAGE + i, "UTF-8"));
            qs.append("&");
        }
        qs.deleteCharAt(qs.length()-1); // delete last useless '&'
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?" + qs.toString());
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        for (int i = 0; i < DEFAULT_MAX_PARAMETERS; ++i) {
            Header[] header = result.getHeaders(QUERY + i);
            Assert.assertEquals(MESSAGE + i, header[0].getValue());
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example #16
Source File: Util.java    From pacbot with Apache License 2.0 6 votes vote down vote up
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 #17
Source File: WebAppContainerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void jsp() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(new HttpGet(server().httpUri() + "/jsp/index.jsp"))) {
            assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
            assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue())
                    .startsWith("text/html");
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity()))
                                                 .replaceAll("");
            assertThat(actualContent).isEqualTo(
                    "<html><body>" +
                    "<p>Hello, Armerian World!</p>" +
                    "<p>Have you heard about the class 'org.slf4j.Logger'?</p>" +
                    "<p>Context path: </p>" + // ROOT context path
                    "<p>Request URI: /index.jsp</p>" +
                    "<p>Scheme: http</p>" +
                    "</body></html>");
        }
    }
}
 
Example #18
Source File: HttpsAdminServerTest.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetUserFromAdminPageNoAccessWithFakeCookie2() throws Exception {
    login(admin.email, admin.pass);

    SSLContext sslcontext = TestUtil.initUnsecuredSSLContext();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new MyHostVerifier());
    CloseableHttpClient httpclient2 = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
            .build();


    String testUser = "[email protected]";
    String appName = "Blynk";
    HttpGet request = new HttpGet(httpsAdminServerUrl + "/users/" + testUser + "-" + appName);
    request.setHeader("Cookie", "session=123");

    try (CloseableHttpResponse response = httpclient2.execute(request)) {
        assertEquals(404, response.getStatusLine().getStatusCode());
    }
}
 
Example #19
Source File: ApacheHttpTransaction.java    From OCR-Equation-Solver with MIT License 6 votes vote down vote up
/**
 * If this returns without throwing, then you can (and must) proceed to reading the
 * content using getResponseAsString() or getResponseAsStream(). If it throws, then
 * you do not have to read. You must always call release().
 *
 * @throws HttpHandlerException
 */
public void execute() throws WAHttpException {

    httpGet = new HttpGet(url.toString());
    HttpHost proxy = proxySettings.getProxyForHttpClient(url.toString());
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
    try {
        response = httpClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK)
            throw new WAHttpException(statusCode);
        entity = response.getEntity();
    } catch (Exception e) {
        // This also releases all resources.
        httpGet.abort();
        if (e instanceof WAHttpException)
            throw (WAHttpException) e;
        else
            throw new WAHttpException(e);
    }
}
 
Example #20
Source File: TableResourceTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Test getting a single table. GET management/tables/{tableName}
 */
@Test
public void testGetTable() throws Exception {
    Map<String, Long> tableCounts = managementService.getTableCount();

    String tableNameToGet = tableCounts.keySet().iterator().next();

    CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TABLE, tableNameToGet)),
            HttpStatus.SC_OK);

    // Check table
    JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
    closeResponse(response);
    assertThat(responseNode).isNotNull();
    assertThatJson(responseNode)
            .isEqualTo("{"
                    + "name: '" + tableNameToGet + "',"
                    + "count: " + tableCounts.get(tableNameToGet) + ","
                    + "url: '" + SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TABLE, tableNameToGet) + "'"
                    + "}");
}
 
Example #21
Source File: HttpConstants.java    From hbc with Apache License 2.0 6 votes vote down vote up
public static HttpUriRequest constructRequest(String host, Endpoint endpoint, Authentication auth) {
  String url = host + endpoint.getURI();
  if (endpoint.getHttpMethod().equalsIgnoreCase(HttpGet.METHOD_NAME)) {
    HttpGet get = new HttpGet(url);
    if (auth != null)
      auth.signRequest(get, null);
    return get;
  } else if (endpoint.getHttpMethod().equalsIgnoreCase(HttpPost.METHOD_NAME) ) {
    HttpPost post = new HttpPost(url);

    post.setEntity(new StringEntity(endpoint.getPostParamString(), Constants.DEFAULT_CHARSET));
    post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
    if (auth != null)
      auth.signRequest(post, endpoint.getPostParamString());

    return post;
  } else {
    throw new IllegalArgumentException("Bad http method: " + endpoint.getHttpMethod());
  }
}
 
Example #22
Source File: JUnitManagedIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenJUnitManagedServer_whenMatchingURL_thenCorrect() throws IOException {
    
    stubFor(get(urlPathMatching("/baeldung/.*"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", APPLICATION_JSON)
                    .withBody("\"testing-library\": \"WireMock\"")));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet request = new HttpGet(String.format("http://localhost:%s/baeldung/wiremock", port));
    HttpResponse httpResponse = httpClient.execute(request);
    String stringResponse = convertHttpResponseToString(httpResponse);

    verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH)));
    assertEquals(200, httpResponse.getStatusLine().getStatusCode());
    assertEquals(APPLICATION_JSON, httpResponse.getFirstHeader("Content-Type").getValue());
    assertEquals("\"testing-library\": \"WireMock\"", stringResponse);
}
 
Example #23
Source File: HttpClientUtil.java    From hermes with Apache License 2.0 6 votes vote down vote up
/**
 * 获取二进制文件流
 * @param url
 * @return
 * @throws Exception
 */
public static ByteArrayOutputStream doFileGetHttps(String url) throws  Exception {
	initSSLContext();
	HttpGet httpGet = new HttpGet(url);
	HttpResponse response = httpClient.execute(httpGet);
	int responseCode = response.getStatusLine().getStatusCode();
	Logger.info("httpClient get 响应状态responseCode="+responseCode+", 请求 URL="+url);
	if(responseCode == RSP_200 || responseCode == RSP_400){
	    InputStream inputStream = response.getEntity().getContent();  
	    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  
	    byte buff[] = new byte[4096];  
	    int counts = 0;  
	    while ((counts = inputStream.read(buff)) != -1) {  
	    	outputStream.write(buff, 0, counts);  
	    }  
	    outputStream.flush();  
	    outputStream.close();  
		httpGet.releaseConnection();
		return outputStream;
	}else{
		httpGet.releaseConnection();
		throw new Exception("接口请求异常:接口响应状态="+responseCode);
	}
}
 
Example #24
Source File: ServiceTicketODataConsumer.java    From C4CODATAAPIDEVGUIDE with Apache License 2.0 6 votes vote down vote up
private Edm readEdm() throws EntityProviderException,
		IllegalStateException, IOException {

	// This is used for both setting the Edm and CSRF Token :)
	if (m_edm != null) {
		return m_edm;
	}

	String serviceUrl = new StringBuilder(getODataServiceUrl())
			.append(SEPARATOR).append(METADATA).toString();

	logger.info("Metadata url => " + serviceUrl);

	final HttpGet get = new HttpGet(serviceUrl);
	get.setHeader(AUTHORIZATION_HEADER, getAuthorizationHeader());
	get.setHeader(CSRF_TOKEN_HEADER, CSRF_TOKEN_FETCH);

	HttpResponse response = getHttpClient().execute(get);

	m_csrfToken = response.getFirstHeader(CSRF_TOKEN_HEADER).getValue();
	logger.info("CSRF token => " + m_csrfToken);

	m_edm = EntityProvider.readMetadata(response.getEntity().getContent(),
			false);
	return m_edm;
}
 
Example #25
Source File: SFTrustManagerIT.java    From snowflake-jdbc with Apache License 2.0 6 votes vote down vote up
private static void accessHost(String host, HttpClient client) throws IOException
{
  final int maxRetry = 10;
  int statusCode = -1;
  for (int retry = 0; retry < maxRetry; ++retry)
  {
    HttpGet httpRequest = new HttpGet(String.format("https://%s:443/", host));
    HttpResponse response = client.execute(httpRequest);
    statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != 503 && statusCode != 504)
    {
      break;
    }
    try
    {
      Thread.sleep(1000L);
    }
    catch (InterruptedException ex)
    {
      // nop
    }
  }
  assertThat(String.format("response code for %s", host),
             statusCode,
             anyOf(equalTo(200), equalTo(403), equalTo(400)));
}
 
Example #26
Source File: DefaultServletCachingTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test
public void testRangeRequest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String fileName = "range.html";
        Path f = tmpDir.resolve(fileName);
        Files.write(f, "hello".getBytes());
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/range.html");
        get.addHeader("range", "bytes=2-3");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("ll", response);

    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example #27
Source File: DouBanHttpGetUtil.java    From JewelCrawler with GNU General Public License v3.0 5 votes vote down vote up
public final static void getByString(List<String> urlList, Connection conn) throws Exception {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        for (String url : urlList) {
                HttpGet httpGet = new HttpGet(url);
                System.out.println("executing request " + httpGet.getURI());

                ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                    public String handleResponse(
                            final HttpResponse response) throws ClientProtocolException, IOException {
                        int status = response.getStatusLine().getStatusCode();
                        System.out.println("------------status:" + status);
                        if (status >= 200 && status < 300) {
                            HttpEntity entity = response.getEntity();
                            return entity != null ? EntityUtils.toString(entity) : null;
                        } else if (status == 300 || status ==301 || status == 302 ||status == 304 || status == 400 ||
                                status == 401 || status == 403 || status == 404 || new String(status + "").startsWith("5")){ //refer to link http://blog.csdn.net/u012043391/article/details/51069441
                            return null;
                        }else {
                            throw new ClientProtocolException("Unexpected response status: " + status);
                        }
                    }
                };

                friendlyToDouban();
                String responseBody = httpClient.execute(httpGet, responseHandler);

                if (responseBody != null) {
                    DouBanParsePage.parseFromString(responseBody, conn);//analyze all links and save into DB
                    DouBanParsePage.extractMovie(url, responseBody, conn);//analyze the page and save into DB if the current page is movie detail page
                    DouBanParsePage.extractComment(url, responseBody, conn);//analyze the page and save into DB if the current page is comment detail page
                }
        }
    } finally {
        httpClient.close();
    }
}
 
Example #28
Source File: ServiceTicketODataConsumer.java    From C4CODATAAPIDEVGUIDE with Apache License 2.0 5 votes vote down vote up
private InputStream executeGet(String absoluteUrl, String contentType)
		throws IllegalStateException, IOException {
	final HttpGet get = new HttpGet(absoluteUrl);
	get.setHeader(AUTHORIZATION_HEADER, getAuthorizationHeader());
	get.setHeader(HTTP_HEADER_ACCEPT, contentType);

	HttpResponse response = getHttpClient().execute(get);
	return response.getEntity().getContent();
}
 
Example #29
Source File: HttpMethodTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
static Stream<Arguments> successfulEmptyRequestCreation()
{
    return Stream.of(
            Arguments.of(HttpMethod.GET, HttpGet.class),
            Arguments.of(HttpMethod.HEAD, HttpHead.class),
            Arguments.of(HttpMethod.DELETE, HttpDelete.class),
            Arguments.of(HttpMethod.OPTIONS, HttpOptions.class),
            Arguments.of(HttpMethod.TRACE, HttpTrace.class),
            Arguments.of(HttpMethod.POST, HttpPost.class),
            Arguments.of(HttpMethod.PUT, HttpPutWithoutBody.class),
            Arguments.of(HttpMethod.DEBUG, HttpDebug.class)
    );
}
 
Example #30
Source File: LayerHttpServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
public InputStream getStream(String baseUrl, RasterLayer layer) throws IOException {
	String url = baseUrl;
	HttpContext context = null;
	if (layer instanceof ProxyLayerSupport) {
		context = new BasicHttpContext();
		// pass url and layer id to the CompositeInterceptor
		context.setAttribute(CompositeInterceptor.BASE_URL, baseUrl);
		context.setAttribute(CompositeInterceptor.LAYER_ID, layer.getId());
		// handle proxy authentication and interceptors
		ProxyLayerSupport proxyLayer = (ProxyLayerSupport) layer;
		ProxyAuthentication authentication = proxyLayer.getProxyAuthentication();
		url = addCredentialsToUrl(baseUrl, authentication);

		// -- add basic authentication
		if (null != authentication
				&& (ProxyAuthenticationMethod.BASIC.equals(authentication.getMethod()) ||
					ProxyAuthenticationMethod.DIGEST.equals(authentication.getMethod()))) {
			// Set up the credentials:
			Credentials creds = new UsernamePasswordCredentials(authentication.getUser(),
					authentication.getPassword());
			URL u = new URL(url);
			AuthScope scope = new AuthScope(u.getHost(), u.getPort(), authentication.getRealm());
			// thread-safe, this is ok
			client.getCredentialsProvider().setCredentials(scope, creds);
		}
	}

	// Create the GET method with the correct URL:
	HttpGet get = new HttpGet(url);

	// Execute the GET:
	HttpResponse response = client.execute(get, context);
	log.debug("Response: {} - {}", response.getStatusLine().getStatusCode(), response.getStatusLine()
			.getReasonPhrase());

	return response.getEntity().getContent();
}