Search in sources :

Example 71 with HttpPost

use of org.apache.http.client.methods.HttpPost in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutorTests method customizeConnectionRequestTimeout.

@Test
public void customizeConnectionRequestTimeout() throws IOException {
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor();
    executor.setConnectionRequestTimeout(7000);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    assertEquals(7000, httpPost.getConfig().getConnectionRequestTimeout());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Test(org.junit.Test)

Example 72 with HttpPost

use of org.apache.http.client.methods.HttpPost in project platform_frameworks_base by android.

the class BandwidthTestUtil method postFileToServer.

/**
     * Post a given file for a given device and timestamp to the server.
     * @param server {@link String} url of test server
     * @param deviceId {@link String} device id that is uploading
     * @param timestamp {@link String} timestamp
     * @param file {@link File} to upload
     * @return true if it succeeded
     */
public static boolean postFileToServer(String server, String deviceId, String timestamp, File file) {
    try {
        Log.d(LOG_TAG, "Uploading begining");
        HttpClient httpClient = new DefaultHttpClient();
        String uri = server;
        if (!uri.endsWith("/")) {
            uri += "/";
        }
        uri += "upload";
        Log.d(LOG_TAG, "Upload url:" + uri);
        HttpPost postRequest = new HttpPost(uri);
        Part[] parts = { new StringPart("device_id", deviceId), new StringPart("timestamp", timestamp), new FilePart("file", file) };
        MultipartEntity reqEntity = new MultipartEntity(parts, postRequest.getParams());
        postRequest.setEntity(reqEntity);
        HttpResponse res = httpClient.execute(postRequest);
        res.getEntity().getContent().close();
    } catch (IOException e) {
        Log.e(LOG_TAG, "Could not upload file with error: " + e);
        return false;
    }
    return true;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(com.android.internal.http.multipart.MultipartEntity) Part(com.android.internal.http.multipart.Part) FilePart(com.android.internal.http.multipart.FilePart) StringPart(com.android.internal.http.multipart.StringPart) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) StringPart(com.android.internal.http.multipart.StringPart) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) FilePart(com.android.internal.http.multipart.FilePart)

Example 73 with HttpPost

use of org.apache.http.client.methods.HttpPost in project androidquery by androidquery.

the class AbstractAjaxCallback method httpPost.

private void httpPost(String url, Map<String, Object> params, AjaxStatus status) throws ClientProtocolException, IOException {
    AQUtility.debug("post", url);
    HttpEntityEnclosingRequestBase req = new HttpPost(url);
    httpEntity(url, req, params, status);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Example 74 with HttpPost

use of org.apache.http.client.methods.HttpPost in project jstorm by alibaba.

the class HttpClientService method excutePost.

protected String excutePost(String url, List<NameValuePair> nvps) throws Exception {
    HttpClient httpClient = getHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    HttpResponse httpResponse = httpClient.execute(httpPost);
    String response = parseResponse(url, httpResponse);
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 75 with HttpPost

use of org.apache.http.client.methods.HttpPost in project jstorm by alibaba.

the class AlimonitorClient method httpPost.

private boolean httpPost(String url, String msg) {
    boolean ret = false;
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {
        HttpPost request = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("name", monitorName));
        nvps.add(new BasicNameValuePair("msg", msg));
        request.setEntity(new UrlEncodedFormEntity(nvps));
        response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            LOG.info(EntityUtils.toString(entity));
        }
        EntityUtils.consume(entity);
        ret = true;
    } catch (Exception e) {
        LOG.error("Exception when sending http request to alimonitor", e);
    } finally {
        try {
            if (response != null)
                response.close();
            httpClient.close();
        } catch (Exception e) {
            LOG.error("Exception when closing httpclient", e);
        }
    }
    return ret;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Aggregations

HttpPost (org.apache.http.client.methods.HttpPost)531 HttpResponse (org.apache.http.HttpResponse)238 StringEntity (org.apache.http.entity.StringEntity)220 Test (org.junit.Test)153 IOException (java.io.IOException)143 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)107 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)99 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)90 ArrayList (java.util.ArrayList)86 NameValuePair (org.apache.http.NameValuePair)84 HttpEntity (org.apache.http.HttpEntity)83 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)68 HttpClient (org.apache.http.client.HttpClient)64 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)63 HttpGet (org.apache.http.client.methods.HttpGet)55 TestHttpClient (io.undertow.testutils.TestHttpClient)54 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JsonNode (com.fasterxml.jackson.databind.JsonNode)39 InputStream (java.io.InputStream)29