Search in sources :

Example 56 with StringEntity

use of org.apache.http.entity.StringEntity in project openhab1-addons by openhab.

the class IhcHttpsClient method sendQ.

private String sendQ(String query, int timeout) throws ClientProtocolException, IOException, NoHttpResponseException {
    logger.trace("Send query (timeout={}): {}", timeout, query);
    postReq.setEntity(new StringEntity(query, "UTF-8"));
    postReq.addHeader("content-type", "text/xml");
    final RequestConfig params = RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(timeout).build();
    postReq.setConfig(params);
    // Execute POST
    HttpResponse response = client.execute(postReq, IhcConnectionPool.getInstance().getHttpContext());
    String resp = EntityUtils.toString(response.getEntity());
    logger.trace("Received response: {}", resp);
    return resp;
}
Also used : StringEntity(org.apache.http.entity.StringEntity) RequestConfig(org.apache.http.client.config.RequestConfig) HttpResponse(org.apache.http.HttpResponse)

Example 57 with StringEntity

use of org.apache.http.entity.StringEntity in project opennms by OpenNMS.

the class HttpUrlConnection method getInputStream.

/* (non-Javadoc)
     * @see java.net.URLConnection#getInputStream()
     */
@Override
public InputStream getInputStream() throws IOException {
    try {
        if (m_clientWrapper == null) {
            connect();
        }
        // Build URL
        int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort();
        URIBuilder ub = new URIBuilder();
        ub.setPort(port);
        ub.setScheme(m_url.getProtocol());
        ub.setHost(m_url.getHost());
        ub.setPath(m_url.getPath());
        if (m_url.getQuery() != null && !m_url.getQuery().trim().isEmpty()) {
            final List<NameValuePair> params = URLEncodedUtils.parse(m_url.getQuery(), StandardCharsets.UTF_8);
            if (!params.isEmpty()) {
                ub.addParameters(params);
            }
        }
        // Build Request
        HttpRequestBase request = null;
        if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) {
            final Content cnt = m_request.getContent();
            HttpPost post = new HttpPost(ub.build());
            ContentType contentType = ContentType.create(cnt.getType());
            LOG.info("Processing POST request for {}", contentType);
            if (contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
                FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData());
                post.setEntity(fields.getEntity());
            } else {
                StringEntity entity = new StringEntity(cnt.getData(), contentType);
                post.setEntity(entity);
            }
            request = post;
        } else {
            request = new HttpGet(ub.build());
        }
        if (m_request != null) {
            // Add Custom Headers
            for (final Header header : m_request.getHeaders()) {
                request.addHeader(header.getName(), header.getValue());
            }
        }
        // Get Response
        CloseableHttpResponse response = m_clientWrapper.execute(request);
        return response.getEntity().getContent();
    } catch (Exception e) {
        throw new IOException("Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e);
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) ContentType(org.apache.http.entity.ContentType) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) StringEntity(org.apache.http.entity.StringEntity) Header(org.opennms.protocols.xml.config.Header) Content(org.opennms.protocols.xml.config.Content) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 58 with StringEntity

use of org.apache.http.entity.StringEntity in project tdi-studio-se by Talend.

the class WsdlTokenManager method getSOAPResponse.

private static String getSOAPResponse(URI issuerUri, String soapEnvelope) {
    HttpResponse response = null;
    // Create the request that will submit the request to the server
    try {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 180000);
        HttpClient client = new SystemDefaultHttpClient(params);
        HttpPost post = new HttpPost(issuerUri);
        StringEntity entity = new StringEntity(soapEnvelope);
        post.setHeader("Content-Type", "application/soap+xml; charset=UTF-8");
        post.setEntity(entity);
        response = client.execute(post);
        return EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
    return null;
}
Also used : SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpClient(org.apache.http.client.HttpClient) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 59 with StringEntity

use of org.apache.http.entity.StringEntity in project tdi-studio-se by Talend.

the class RestClient method executePutRequest.

private HttpResponse executePutRequest(String apiURI, String payloadAsString) {
    try {
        HttpPut putRequest = new HttpPut(bonitaURI + apiURI);
        putRequest.addHeader("Content-Type", "application/json");
        StringEntity input = new StringEntity(payloadAsString);
        input.setContentType("application/json");
        putRequest.setEntity(input);
        return httpClient.execute(putRequest, httpContext);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPut(org.apache.http.client.methods.HttpPut) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) IOException(java.io.IOException)

Example 60 with StringEntity

use of org.apache.http.entity.StringEntity in project tdi-studio-se by Talend.

the class RestClient method executePostRequest.

private HttpResponse executePostRequest(String apiURI, String payloadAsString) {
    try {
        HttpPost postRequest = new HttpPost(bonitaURI + apiURI);
        StringEntity input = new StringEntity(payloadAsString);
        input.setContentType("application/json");
        postRequest.setEntity(input);
        HttpResponse response = httpClient.execute(postRequest, httpContext);
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) IOException(java.io.IOException)

Aggregations

StringEntity (org.apache.http.entity.StringEntity)456 HttpPost (org.apache.http.client.methods.HttpPost)249 HttpResponse (org.apache.http.HttpResponse)147 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)141 Test (org.junit.Test)105 HttpPut (org.apache.http.client.methods.HttpPut)94 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)88 IOException (java.io.IOException)84 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)68 HttpEntity (org.apache.http.HttpEntity)60 JsonNode (com.fasterxml.jackson.databind.JsonNode)54 Deployment (org.activiti.engine.test.Deployment)50 TestHttpClient (io.undertow.testutils.TestHttpClient)30 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)27 StatusLine (org.apache.http.StatusLine)27 HttpGet (org.apache.http.client.methods.HttpGet)27 Gson (com.google.gson.Gson)24 UnsupportedEncodingException (java.io.UnsupportedEncodingException)24 ProtocolVersion (org.apache.http.ProtocolVersion)24 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)23