Example usage for org.apache.http.impl.client DefaultHttpClient execute

List of usage examples for org.apache.http.impl.client DefaultHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:org.opensourcetlapp.tl.TLLib.java

public static void subscribeThread(String topicId) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.setCookieStore(cookieStore);

    HttpPost httpost = new HttpPost(SUB_URL);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("action", "toggleSub"));
    nvps.add(new BasicNameValuePair("thread_id", topicId));
    nvps.add(new BasicNameValuePair("token", tokenField));
    Log.d(TAG, "Subscribing Thread");

    try {//from   w w  w  .j  a  v  a2 s  .c  om
        httpost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            entity.consumeContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    }
}

From source file:org.sociotech.fishification.utils.CallHttpTask.java

@Override
protected String doInBackground(String... urls) {
    String response = "";
    for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {/*from  w w  w  .j a v  a  2  s . com*/

            client.execute(httpGet);

        } catch (Exception e) {
            Log.e("HTTP Response", "Error executing HTTP GET.");
        }
    }
    return response;
}

From source file:se.vgregion.pubsub.inttest.Publisher.java

public void publish(URI hub, Feed feed, String... urls) throws URISyntaxException, IOException {
    this.feed = feed;

    HttpPost post = new HttpPost(hub);

    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("hub.mode", "publish"));
    for (String url : urls) {
        parameters.add(new BasicNameValuePair("hub.url", url));
    }//from  ww w.  j av  a  2  s.c  o  m

    post.setEntity(new UrlEncodedFormEntity(parameters));

    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);

    Assert.assertEquals(204, response.getStatusLine().getStatusCode());
}

From source file:co.cask.cdap.internal.app.services.http.AppFabricTestBase.java

protected static HttpResponse doPost(String resource, String body, Header[] headers) throws Exception {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(getEndPoint(resource));

    if (body != null) {
        post.setEntity(new StringEntity(body));
    }//from  www .  ja  v  a 2  s.  c  om

    if (headers != null) {
        post.setHeaders(ObjectArrays.concat(AUTH_HEADER, headers));
    } else {
        post.setHeader(AUTH_HEADER);
    }
    return client.execute(post);
}

From source file:com.jaeksoft.searchlib.test.legacy.CommonTestCase.java

public InputStream getResponse(HttpPost httpPost) throws IllegalStateException, SAXException, IOException,
        ParserConfigurationException, XPathExpressionException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse httpResponse = httpClient.execute(httpPost);
    return httpResponse.getEntity().getContent();

}

From source file:com.jaeksoft.searchlib.test.legacy.CommonTestCase.java

public String getHttpResponse(HttpPost httpPost, String xpath) throws IllegalStateException, SAXException,
        IOException, ParserConfigurationException, XPathExpressionException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse httpResponse = httpClient.execute(httpPost);
    XPathParser parser = new XPathParser(new InputSource(httpResponse.getEntity().getContent()));
    return parser.getNodeString(xpath);

}

From source file:org.wso2.carbon.dynamic.client.web.app.registration.util.RemoteDCRClient.java

public static OAuthApplicationInfo createOAuthApplication(RegistrationProfile registrationProfile, String host)
        throws DynamicClientRegistrationException {
    if (log.isDebugEnabled()) {
        log.debug("Invoking DCR service to create OAuth application for web app : "
                + registrationProfile.getClientName());
    }//from  ww  w. j  a  v a  2s  .com
    DefaultHttpClient httpClient = getHTTPSClient();
    String clientName = registrationProfile.getClientName();
    try {
        URI uri = new URIBuilder().setScheme(
                DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_PROTOCOL)
                .setHost(host)
                .setPath(
                        DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
                .build();
        Gson gson = new Gson();
        StringEntity entity = new StringEntity(gson.toJson(registrationProfile),
                DynamicClientWebAppRegistrationConstants.ContentTypes.CONTENT_TYPE_APPLICATION_JSON,
                DynamicClientWebAppRegistrationConstants.CharSets.CHARSET_UTF8);
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);
        int status = response.getStatusLine().getStatusCode();
        HttpEntity responseData = response.getEntity();
        String responseString = EntityUtils.toString(responseData,
                DynamicClientWebAppRegistrationConstants.CharSets.CHARSET_UTF8);
        if (status != 201) {
            String msg = "Backend server error occurred while invoking DCR endpoint for "
                    + "registering service-provider upon web-app : '" + clientName
                    + "'; Server returned response '" + responseString + "' with HTTP status code '" + status
                    + "'";
            throw new DynamicClientRegistrationException(msg);
        }
        return getOAuthApplicationInfo(gson.fromJson(responseString, JsonElement.class));
    } catch (URISyntaxException e) {
        throw new DynamicClientRegistrationException(
                "Exception occurred while constructing the URI for invoking "
                        + "DCR endpoint for registering service-provider for web-app : " + clientName,
                e);
    } catch (UnsupportedEncodingException e) {
        throw new DynamicClientRegistrationException(
                "Exception occurred while constructing the payload for invoking "
                        + "DCR endpoint for registering service-provider for web-app : " + clientName,
                e);
    } catch (IOException e) {
        throw new DynamicClientRegistrationException("Connection error occurred while invoking DCR endpoint for"
                + " registering service-provider for web-app : " + clientName, e);
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
}

From source file:org.wso2.carbon.esb.nhttp.transport.test.DefaultRequestContentTypeTestCase.java

@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
@Test(groups = { "wso2.esb" }, description = "Test for DEFAULT_REQUEST_TYPE for nhttp transport")
public void testReturnContentType() throws Exception {
    try {//from w w w .j a va  2s .c om
        String url = getApiInvocationURL("defaultRequestContentTypeApi").concat("?symbol=wso2");
        HttpUriRequest request = new HttpPut(url);
        DefaultHttpClient client = new DefaultHttpClient();

        HttpResponse response = client.execute(request);
        Assert.assertNotNull(response);
        Assert.assertTrue(getResponse(response).toString().contains("WSO2"));
    } catch (IOException e) {
        log.error("Error while sending the request to the endpoint. ", e);
    }

}

From source file:a.org.fakereplace.integration.wildfly.resteasy.changepath.ChangeJAXRSPathTestCase.java

@Test
public void testChangingJaxbModel() throws Exception {

    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(new HttpGet(url + "hellopath/helloworld"));
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    String result = HttpUtils.getContent(response);
    Assert.assertEquals("root", result);
    RemoteClassReplacer r = new RemoteClassReplacer();
    r.queueClassForReplacement(HelloWorldResource.class, HelloWorldResource1.class);
    r.replaceQueuedClasses(DEPLOYMENT_NAME);

    response = client.execute(new HttpGet(url + "hellopath/helloworld"));
    Assert.assertEquals(404, response.getStatusLine().getStatusCode());
    result = HttpUtils.getContent(response);

    response = client.execute(new HttpGet(url + "hellopath/helloworld/sub"));
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    result = HttpUtils.getContent(response);
    Assert.assertEquals("sub", result);

    r = new RemoteClassReplacer();
    r.queueClassForReplacement(HelloWorldResource.class, HelloWorldResource2.class);
    r.replaceQueuedClasses(DEPLOYMENT_NAME);

    response = client.execute(new HttpGet(url + "hellopath/helloworld"));
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    result = HttpUtils.getContent(response);
    Assert.assertEquals("root", result);

    response = client.execute(new HttpGet(url + "hellopath/helloworld/sub"));
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    result = HttpUtils.getContent(response);
    Assert.assertEquals("sub", result);
}

From source file:eu.trentorise.smartcampus.storage.ResourceRetriever.java

private byte[] getFileContent(Token token) throws ClientProtocolException, IOException {
    HttpUriRequest request = null;//from  w  w w  .ja  v  a2 s  . co  m
    if (token.getUrl() != null && token.getMethodREST() != null) {
        if (token.getMethodREST().equals("GET")) {
            request = new HttpGet(token.getUrl());
        } else if (token.getMethodREST().equals("POST")) {
            request = new HttpPost(token.getUrl());
        } else if (token.getMethodREST().equals("PUT")) {
            request = new HttpPut(token.getUrl());
        } else if (token.getMethodREST().equals("DELETE")) {
            request = new HttpDelete(token.getUrl());
        }

        if (token.getHttpHeaders() != null) {
            for (Entry<String, String> entry : token.getHttpHeaders().entrySet()) {
                request.setHeader(entry.getKey(), entry.getValue());
            }
        }
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(request);
        if (response.getStatusLine().getStatusCode() == 200) {
            InputStream is = response.getEntity().getContent();
            return eu.trentorise.smartcampus.storage.Utils.read(is);
        }
    } else if (token.getMetadata() != null) {
        return retrieveContent(token.getMetadata());
    }
    return null;
}