Search in sources :

Example 96 with HttpResponse

use of org.apache.http.HttpResponse in project azure-tools-for-java by Microsoft.

the class SparkRestUtil method getEntity.

//    @Nullable
//    public static List<Application> getApplications(@NotNull ClusterDetail clusterDetail) throws HDIException, IOException {
//        HttpEntity entity = getEntity(clusterDetail, "applications");
//        String entityType = entity.getContentType().getValue();
//        if( entityType.equals("application/json")){
//            String json = EntityUtils.toString(entity);
//            List<Application> apps = objectMapper.readValue(json, TypeFactory.defaultInstance().constructType(List.class, Application.class));
//            return apps;
//        }
//        return null;
//    }
public static HttpEntity getEntity(@NotNull IClusterDetail clusterDetail, @NotNull String restUrl) throws HDIException, IOException {
    provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(clusterDetail.getHttpUserName(), clusterDetail.getHttpPassword()));
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
    String url = String.format(SPARK_REST_API_ENDPOINT, clusterDetail.getName(), restUrl);
    HttpGet get = new HttpGet(url);
    HttpResponse response = client.execute(get);
    int code = response.getStatusLine().getStatusCode();
    if (code == HttpStatus.SC_OK || code == HttpStatus.SC_CREATED) {
        return response.getEntity();
    } else {
        throw new HDIException(response.getStatusLine().getReasonPhrase(), response.getStatusLine().getStatusCode());
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HDIException(com.microsoft.azure.hdinsight.sdk.common.HDIException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 97 with HttpResponse

use of org.apache.http.HttpResponse in project wildfly by wildfly.

the class WebSecurityExternalAuthTestCase method makeCall.

protected void makeCall(String user, int expectedStatusCode) throws Exception {
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpget = new HttpGet(url.toExternalForm() + "secured/");
        httpget.addHeader("User", user);
        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
        EntityUtils.consume(entity);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 98 with HttpResponse

use of org.apache.http.HttpResponse in project undertow by undertow-io.

the class SimpleConfidentialRedirectTestCase method sendRequest.

private void sendRequest(TestHttpClient client, String uri) throws IOException {
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + uri);
    HttpResponse result = client.execute(get);
    Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    Assert.assertEquals("https", result.getFirstHeader("scheme").getValue());
    Assert.assertEquals(uri, result.getFirstHeader("uri").getValue());
    HttpClientUtils.readResponse(result);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 99 with HttpResponse

use of org.apache.http.HttpResponse in project wechat-mp-sdk by usc.

the class MediaUtil method getMedia.

public static File getMedia(License license, String mediaId, String path) {
    if (StringUtils.isEmpty(mediaId) || StringUtils.isEmpty(path)) {
        return null;
    }
    String accessToken = AccessTokenUtil.getAccessToken(license);
    String url = WechatRequest.GET_MEDIA.getUrl();
    try {
        URI uri = new URIBuilder(url).setParameter("access_token", accessToken).setParameter("media_id", mediaId).build();
        HttpResponse response = Request.Get(uri).connectTimeout(HttpUtil.CONNECT_TIMEOUT).socketTimeout(HttpUtil.SOCKET_TIMEOUT).execute().returnResponse();
        return downloadFile(response, mediaId, path, uri);
    } catch (Exception e) {
        String msg = "get media failed:\n " + "url=" + url + "?access_token=" + accessToken + "&media_id=" + mediaId;
        log.error(msg, e);
        return null;
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 100 with HttpResponse

use of org.apache.http.HttpResponse in project undertow by undertow-io.

the class FormDataParserTestCase method runTest.

private void runTest(final NameValuePair... pairs) throws Exception {
    DefaultServer.setRootHandler(rootHandler);
    TestHttpClient client = new TestHttpClient();
    try {
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        post.setHeader(Headers.CONTENT_TYPE_STRING, FormEncodedDataDefinition.APPLICATION_X_WWW_FORM_URLENCODED);
        post.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        checkResult(data, result);
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient)

Aggregations

HttpResponse (org.apache.http.HttpResponse)4168 Test (org.junit.Test)2110 HttpGet (org.apache.http.client.methods.HttpGet)1770 IOException (java.io.IOException)1000 URI (java.net.URI)817 HttpPost (org.apache.http.client.methods.HttpPost)699 HttpClient (org.apache.http.client.HttpClient)550 HttpEntity (org.apache.http.HttpEntity)496 TestHttpClient (io.undertow.testutils.TestHttpClient)403 InputStream (java.io.InputStream)373 Header (org.apache.http.Header)370 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)333 StringEntity (org.apache.http.entity.StringEntity)332 HttpPut (org.apache.http.client.methods.HttpPut)310 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)307 ArrayList (java.util.ArrayList)296 Identity (org.olat.core.id.Identity)262 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)237 File (java.io.File)192 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)191