Search in sources :

Example 61 with HttpPost

use of org.apache.http.client.methods.HttpPost in project android-uploader by nightscout.

the class RestV1UploaderTest method testCalRecord_Entity.

@Test
public void testCalRecord_Entity() throws Exception {
    preferences.setCalibrationUploadEnabled(true);
    restUploader.uploadCalRecords(Lists.newArrayList(mockCalRecord()));
    HttpPost post = (HttpPost) captor.getValue();
    String entity = CharStreams.toString(new InputStreamReader(post.getEntity().getContent()));
    verifyCalRecord(new JSONObject(entity));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 62 with HttpPost

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

the class NinjaTestBrowser method postJson.

public String postJson(String url, Object object) {
    try {
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost post = new HttpPost(url);
        StringEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(object), "utf-8");
        entity.setContentType("application/json; charset=utf-8");
        post.setEntity(entity);
        post.releaseConnection();
        // Here we go!
        return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException)

Example 63 with HttpPost

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

the class NinjaTestBrowser method postXml.

public String postXml(String url, Object object) {
    try {
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost post = new HttpPost(url);
        StringEntity entity = new StringEntity(new XmlMapper().writeValueAsString(object), "utf-8");
        entity.setContentType("application/xml; charset=utf-8");
        post.setEntity(entity);
        post.releaseConnection();
        // Here we go!
        return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 64 with HttpPost

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

the class NinjaTestBrowser method uploadFile.

public String uploadFile(String url, String paramName, File fileToUpload) {
    String response = null;
    try {
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost post = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        // For File parameters
        entity.addPart(paramName, new FileBody((File) fileToUpload));
        post.setEntity(entity);
        // Here we go!
        response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
        post.releaseConnection();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) File(java.io.File) IOException(java.io.IOException)

Example 65 with HttpPost

use of org.apache.http.client.methods.HttpPost in project openhab1-addons by openhab.

the class StreamClientImpl method createHttpRequest.

protected HttpUriRequest createHttpRequest(UpnpMessage upnpMessage, UpnpRequest upnpRequestOperation) throws MethodNotSupportedException {
    switch(upnpRequestOperation.getMethod()) {
        case GET:
            return new HttpGet(upnpRequestOperation.getURI());
        case SUBSCRIBE:
            return new HttpGet(upnpRequestOperation.getURI()) {

                @Override
                public String getMethod() {
                    return UpnpRequest.Method.SUBSCRIBE.getHttpName();
                }
            };
        case UNSUBSCRIBE:
            return new HttpGet(upnpRequestOperation.getURI()) {

                @Override
                public String getMethod() {
                    return UpnpRequest.Method.UNSUBSCRIBE.getHttpName();
                }
            };
        case POST:
            HttpEntityEnclosingRequest post = new HttpPost(upnpRequestOperation.getURI());
            post.setEntity(createHttpRequestEntity(upnpMessage));
            // Fantastic API
            return (HttpUriRequest) post;
        case NOTIFY:
            HttpEntityEnclosingRequest notify = new HttpPost(upnpRequestOperation.getURI()) {

                @Override
                public String getMethod() {
                    return UpnpRequest.Method.NOTIFY.getHttpName();
                }
            };
            notify.setEntity(createHttpRequestEntity(upnpMessage));
            // Fantastic API
            return (HttpUriRequest) notify;
        default:
            throw new MethodNotSupportedException(upnpRequestOperation.getHttpMethodName());
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) MethodNotSupportedException(org.apache.http.MethodNotSupportedException)

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