Search in sources :

Example 6 with NameValuePair

use of org.apache.http.NameValuePair 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 7 with NameValuePair

use of org.apache.http.NameValuePair in project api-snippets by TwilioDevEd.

the class TwilioTest method main.

public static void main(String[] args) throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
    // Build the parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("To", "+15558675309"));
    params.add(new BasicNameValuePair("From", "+15017250604"));
    params.add(new BasicNameValuePair("Body", "This is the ship that made the Kessel Run in fourteen parsecs?"));
    MessageFactory messageFactory = client.getAccount().getMessageFactory();
    Message message = messageFactory.create(params);
    System.out.println(message.getSid());
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Example 8 with NameValuePair

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

the class paloconnection method logout.

// Logouts the current palo User
public void logout() throws paloexception {
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("sid", this.strToken));
    try {
        HttpEntity entity = sendToServer(qparams, "/server/logout");
        CSVReader csv = new CSVReader(entity.getContent(), ';', "UTF-8");
        // CsvReader csv = new CsvReader(sendToServer(qparams, "/server/logout").getContent(),
        // Charset.defaultCharset());
        csv.setQuoteChar('"');
        csv.readNext();
        csv.close();
        entity.consumeContent();
    // paloHttpClient.getConnectionManager().shutdown();
    } catch (Exception e) {
        throw new paloexception(e.getMessage());
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) CSVReader(com.talend.csv.CSVReader) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Example 9 with NameValuePair

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

the class paloconnection method getRulefunctions.

public String getRulefunctions() throws paloexception {
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("sid", getPaloToken()));
    try {
        StringBuilder sb = new StringBuilder();
        HttpEntity entity = sendToServer(qparams, "/rule/functions");
        CSVReader csv = new CSVReader(entity.getContent(), ';', "UTF-8");
        csv.setQuoteChar('"');
        while (csv.readNext()) {
            sb.append(csv.get(0));
        }
        csv.close();
        entity.consumeContent();
        return sb.toString();
    } catch (Exception e) {
        throw new paloexception(e.getMessage());
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) CSVReader(com.talend.csv.CSVReader) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Example 10 with NameValuePair

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

the class paloconnection method load.

// Load the Server
public void load() throws paloexception {
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("sid", this.strToken));
    sendToServerSingleRC(qparams, "/server/load");
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Aggregations

NameValuePair (org.apache.http.NameValuePair)725 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)603 ArrayList (java.util.ArrayList)486 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)265 HttpPost (org.apache.http.client.methods.HttpPost)228 HttpResponse (org.apache.http.HttpResponse)157 IOException (java.io.IOException)131 HttpEntity (org.apache.http.HttpEntity)116 Test (org.junit.Test)85 URI (java.net.URI)82 Map (java.util.Map)73 HashMap (java.util.HashMap)69 HttpGet (org.apache.http.client.methods.HttpGet)67 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)61 UnsupportedEncodingException (java.io.UnsupportedEncodingException)60 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)57 URISyntaxException (java.net.URISyntaxException)54 URIBuilder (org.apache.http.client.utils.URIBuilder)52 Document (org.jsoup.nodes.Document)51 HttpClient (org.apache.http.client.HttpClient)50