Search in sources :

Example 61 with NameValuePair

use of org.apache.http.NameValuePair in project xUtils by wyouflf.

the class URLEncodedUtils method format.

/**
     * Returns a String that is suitable for use as an <code>application/x-www-form-urlencoded</code>
     * list of parameters in an HTTP PUT or HTTP POST.
     *
     * @param parameters The parameters to include.
     * @param charset    The encoding to use.
     */
public static String format(final List<? extends NameValuePair> parameters, final String charset) {
    final StringBuilder result = new StringBuilder();
    for (final NameValuePair parameter : parameters) {
        final String encodedName = encodeFormFields(parameter.getName(), charset);
        final String encodedValue = encodeFormFields(parameter.getValue(), charset);
        if (result.length() > 0) {
            result.append(PARAMETER_SEPARATOR);
        }
        result.append(encodedName);
        if (encodedValue != null) {
            result.append(NAME_VALUE_SEPARATOR);
            result.append(encodedValue);
        }
    }
    return result.toString();
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair)

Example 62 with NameValuePair

use of org.apache.http.NameValuePair in project hadoop by apache.

the class PseudoAuthenticationHandler method getUserName.

private String getUserName(HttpServletRequest request) {
    String queryString = request.getQueryString();
    if (queryString == null || queryString.length() == 0) {
        return null;
    }
    List<NameValuePair> list = URLEncodedUtils.parse(queryString, UTF8_CHARSET);
    if (list != null) {
        for (NameValuePair nv : list) {
            if (PseudoAuthenticator.USER_NAME.equals(nv.getName())) {
                return nv.getValue();
            }
        }
    }
    return null;
}
Also used : NameValuePair(org.apache.http.NameValuePair)

Example 63 with NameValuePair

use of org.apache.http.NameValuePair in project robovm by robovm.

the class URLEncodedUtils method format.

/**
     * Returns a String that is suitable for use as an <code>application/x-www-form-urlencoded</code>
     * list of parameters in an HTTP PUT or HTTP POST.
     * 
     * @param parameters  The parameters to include.
     * @param encoding The encoding to use.
     */
public static String format(final List<? extends NameValuePair> parameters, final String encoding) {
    final StringBuilder result = new StringBuilder();
    for (final NameValuePair parameter : parameters) {
        final String encodedName = encode(parameter.getName(), encoding);
        final String value = parameter.getValue();
        final String encodedValue = value != null ? encode(value, encoding) : "";
        if (result.length() > 0)
            result.append(PARAMETER_SEPARATOR);
        result.append(encodedName);
        result.append(NAME_VALUE_SEPARATOR);
        result.append(encodedValue);
    }
    return result.toString();
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair)

Example 64 with NameValuePair

use of org.apache.http.NameValuePair in project cryptonomica by Cryptonomica.

the class TwilioUtils method sendSms.

public static Message sendSms(final String phoneNumber, final String smsMessage) throws NumberParseException, IllegalArgumentException, TwilioRestException {
    checkPhoneNumber(phoneNumber);
    checkSmsMessage(smsMessage);
    final String accountSid = ApiKeysUtils.getApiKey("twilioAccountSid");
    final String authToken = ApiKeysUtils.getApiKey("twilioAuthToken");
    final String twilioPhoneNumber = ApiKeysUtils.getApiKey("twilioPhoneNumber");
    // Twilio.init(accountSid, authToken);
    // Message message = Message.creator(
    // new PhoneNumber(phoneNumber),        // To number
    // new PhoneNumber(twilioPhoneNumber),  // From number
    // smsMessage                           // SMS body
    // ).create();
    // LOG.warning(message.toString());
    TwilioRestClient client = new TwilioRestClient(accountSid, authToken);
    Account account = client.getAccount();
    MessageFactory messageFactory = account.getMessageFactory();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("To", phoneNumber));
    params.add(new BasicNameValuePair("From", twilioPhoneNumber));
    params.add(new BasicNameValuePair("Body", smsMessage));
    Message sms = messageFactory.create(params);
    LOG.warning(sms.toString());
    return sms;
}
Also used : Account(com.twilio.sdk.resource.instance.Account) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) MessageFactory(com.twilio.sdk.resource.factory.MessageFactory) Message(com.twilio.sdk.resource.instance.Message) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) TwilioRestClient(com.twilio.sdk.TwilioRestClient)

Example 65 with NameValuePair

use of org.apache.http.NameValuePair in project crawler4j by yasserg.

the class PageFetcher method doFormLogin.

/**
 * FORM authentication<br/>
 * Official Example: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org
 * /apache/http/examples/client/ClientFormLogin.java
 */
private void doFormLogin(FormAuthInfo authInfo) {
    logger.info("FORM authentication for: {}", authInfo.getLoginTarget());
    String fullUri = authInfo.getProtocol() + "://" + authInfo.getHost() + ":" + authInfo.getPort() + authInfo.getLoginTarget();
    HttpPost httpPost = new HttpPost(fullUri);
    List<NameValuePair> formParams = new ArrayList<>();
    formParams.add(new BasicNameValuePair(authInfo.getUsernameFormStr(), authInfo.getUsername()));
    formParams.add(new BasicNameValuePair(authInfo.getPasswordFormStr(), authInfo.getPassword()));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, StandardCharsets.UTF_8);
    httpPost.setEntity(entity);
    try {
        httpClient.execute(httpPost);
        logger.debug("Successfully request to login in with user: {} to: {}", authInfo.getUsername(), authInfo.getHost());
    } catch (ClientProtocolException e) {
        logger.error("While trying to login to: {} - Client protocol not supported", authInfo.getHost(), e);
    } catch (IOException e) {
        logger.error("While trying to login to: {} - Error making request", authInfo.getHost(), e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

NameValuePair (org.apache.http.NameValuePair)713 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)592 ArrayList (java.util.ArrayList)478 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)256 HttpPost (org.apache.http.client.methods.HttpPost)218 HttpResponse (org.apache.http.HttpResponse)150 IOException (java.io.IOException)125 HttpEntity (org.apache.http.HttpEntity)108 Test (org.junit.Test)85 URI (java.net.URI)79 Map (java.util.Map)72 HashMap (java.util.HashMap)68 HttpGet (org.apache.http.client.methods.HttpGet)66 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)59 UnsupportedEncodingException (java.io.UnsupportedEncodingException)58 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)55 URISyntaxException (java.net.URISyntaxException)52 Document (org.jsoup.nodes.Document)51 URIBuilder (org.apache.http.client.utils.URIBuilder)50 HttpClient (org.apache.http.client.HttpClient)46