Search in sources :

Example 41 with HttpPost

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

the class TestNanolets method doSomeBasicMethodTest.

@Test
public void doSomeBasicMethodTest() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:9090/user/blabla");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: GET<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
    HttpPost httppost = new HttpPost("http://localhost:9090/user/blabla");
    response = httpclient.execute(httppost);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: POST<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
    HttpPut httpgput = new HttpPut("http://localhost:9090/user/blabla");
    response = httpclient.execute(httpgput);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: PUT<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
    HttpDelete httpdelete = new HttpDelete("http://localhost:9090/user/blabla");
    response = httpclient.execute(httpdelete);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: DELETE<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 42 with HttpPost

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

the class ApacheHttpClient method post.

public ApiResponse post(String requestURL, ApiRequest message) throws IOException, SignatureException {
    log.debug("POST url: {}", requestURL);
    log.debug("POST content: {}", message == null ? "(empty)" : message.content);
    HttpPost request = new HttpPost(requestURL);
    if (message != null && message.content != null) {
        request.setEntity(new StringEntity(message.content, ContentType.create(message.contentType.toString(), "UTF-8")));
    }
    HttpResponse httpResponse = httpClient.execute(request);
    ApiResponse apiResponse = readResponse(httpResponse);
    request.releaseConnection();
    return apiResponse;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse)

Example 43 with HttpPost

use of org.apache.http.client.methods.HttpPost in project openkit-android by OpenKit.

the class AsyncHttpClient method post.

/**
     * Perform a HTTP POST request and track the Android Context which initiated
     * the request. Set headers only for this request
     *
     * @param context the Android Context which initiated the request.
     * @param url the URL to send the request to.
     * @param headers set headers only for this request
     * @param entity a raw {@link HttpEntity} to send with the request, for
     *        example, use this to send string/json/xml payloads to a server by
     *        passing a {@link org.apache.http.entity.StringEntity}.
     * @param contentType the content type of the payload you are sending, for
     *        example application/json if sending a json payload.
     * @param responseHandler the response handler instance that should handle
     *        the response.
     */
public void post(Context context, String url, Header[] headers, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
    HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPost(url), entity);
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Example 44 with HttpPost

use of org.apache.http.client.methods.HttpPost in project openkit-android by OpenKit.

the class AsyncHttpClient method post.

/**
     * Perform a HTTP POST request and track the Android Context which initiated
     * the request. Set headers only for this request
     * 
     * @param context the Android Context which initiated the request.
     * @param url the URL to send the request to.
     * @param headers set headers only for this request
     * @param params additional POST parameters to send with the request.
     * @param contentType the content type of the payload you are sending, for
     *        example application/json if sending a json payload.
     * @param responseHandler the response handler instance that should handle
     *        the response.
     */
public void post(Context context, String url, Header[] headers, RequestParams params, String contentType, AsyncHttpResponseHandler responseHandler) {
    HttpEntityEnclosingRequestBase request = new HttpPost(url);
    if (params != null)
        request.setEntity(paramsToEntity(params));
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Example 45 with HttpPost

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

the class HttpRequestExecuterImpl method postContent.

public HttpResponse postContent(String url, String content, String contentType) throws HttpRequestException {
    try {
        StringEntity stringEntity = new StringEntity(content, FormConstants.CHAR_ENC_DEFAULT);
        stringEntity.setContentType(contentType);
        HttpPost httppost = new HttpPost(url);
        httppost.setHeader(FormConstants.CONTENT_TYPE, contentType);
        httppost.setEntity(stringEntity);
        return execute(httppost);
    } catch (Exception e) {
        throw new HttpRequestException(e);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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