Search in sources :

Example 96 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project selenium-tests by Wikia.

the class CommonUtils method sendPost.

public static String sendPost(String apiUrl, String[][] param) {
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(apiUrl);
        List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
        for (int i = 0; i < param.length; i++) {
            paramPairs.add(new BasicNameValuePair(param[i][0], param[i][1]));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(paramPairs));
        HttpResponse response = null;
        response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity);
    } catch (UnsupportedEncodingException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    } catch (ClientProtocolException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    } catch (IOException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 97 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project appsly-android-rest by 47deg.

the class JacksonHttpFormValuesConverter method toRequestBody.

@Override
@SuppressWarnings("unchecked")
public <T> HttpEntity toRequestBody(T object, String contentType) {
    Logger.d("JacksonHttpFormValuesConverter.toRequestBody: object: " + object);
    try {
        HttpEntity entity = null;
        List<NameValuePair> vals = new ArrayList<NameValuePair>();
        if (HeaderUtils.CONTENT_TYPE_FORM_URL_ENCODED.startsWith(contentType)) {
            Map<String, Object> props = mapper.convertValue(object, Map.class);
            for (Map.Entry<String, Object> formPartEntry : props.entrySet()) {
                if (formPartEntry.getValue() != null) {
                    vals.add(new BasicNameValuePair(formPartEntry.getKey(), formPartEntry.getValue().toString()));
                }
            }
            entity = new UrlEncodedFormEntity(vals);
        } else if (HeaderUtils.CONTENT_TYPE_MULTIPART_FORM_DATA.startsWith(contentType)) {
            Map<String, Object> props = mapper.convertValue(object, Map.class);
            MultipartEntity multipartEntity = new MultipartEntity(null);
            for (Map.Entry<String, Object> formPartEntry : props.entrySet()) {
                if (formPartEntry.getValue() != null) {
                    if (formPartEntry.getValue() instanceof FileFormField) {
                        FileFormField fileFormField = (FileFormField) formPartEntry.getValue();
                        multipartEntity.addPart(formPartEntry.getKey(), fileFormField.getFile(), fileFormField.getContentType());
                    } else {
                        multipartEntity.addPart(formPartEntry.getKey(), formPartEntry.getValue().toString());
                    }
                }
            }
            entity = multipartEntity;
        }
        return entity;
    } catch (UnsupportedEncodingException e) {
        throw new SerializationException(e);
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) SerializationException(ly.apps.android.rest.exceptions.SerializationException) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Example 98 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project SeaStar by 13120241790.

the class RequestParams method createMultipartEntity.

private HttpEntity createMultipartEntity(ResponseHandlerInterface progressHandler) throws IOException {
    SimpleMultipartEntity entity = new SimpleMultipartEntity(progressHandler);
    entity.setIsRepeatable(isRepeatable);
    // Add string params
    for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
        entity.addPart(entry.getKey(), entry.getValue());
    }
    // Add non-string params
    List<BasicNameValuePair> params = getParamsList(null, urlParamsWithObjects);
    for (BasicNameValuePair kv : params) {
        entity.addPart(kv.getName(), kv.getValue());
    }
    // Add stream params
    for (ConcurrentHashMap.Entry<String, StreamWrapper> entry : streamParams.entrySet()) {
        StreamWrapper stream = entry.getValue();
        if (stream.inputStream != null) {
            entity.addPart(entry.getKey(), stream.name, stream.inputStream, stream.contentType);
        }
    }
    // Add file params
    for (ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
        FileWrapper fileWrapper = entry.getValue();
        entity.addPart(entry.getKey(), fileWrapper.file, fileWrapper.contentType);
    }
    return entity;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 99 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project DrupalCloud by INsReady.

the class JSONServerClient method userLogin.

@Override
public String userLogin(String username, String password) throws ServiceNotAvailableException {
    BasicNameValuePair[] parameters = new BasicNameValuePair[2];
    parameters[0] = new BasicNameValuePair("username", username);
    parameters[1] = new BasicNameValuePair("password", password);
    return call("user.login", parameters);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 100 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project DrupalCloud by INsReady.

the class JSONServerClient method flagIsFlagged.

@Override
public boolean flagIsFlagged(String flagName, int contentId, int uid) throws ServiceNotAvailableException {
    BasicNameValuePair[] parameters = new BasicNameValuePair[3];
    parameters[0] = new BasicNameValuePair("flag_name", flagName);
    parameters[1] = new BasicNameValuePair("content_id", String.valueOf(contentId));
    parameters[2] = new BasicNameValuePair("uid", String.valueOf(uid));
    String result = call("flag.is_flagged", parameters);
    JSONObject jso;
    try {
        jso = new JSONObject(result);
        boolean flag = jso.getBoolean("#data");
        return flag;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) JSONException(org.json.JSONException)

Aggregations

BasicNameValuePair (org.apache.http.message.BasicNameValuePair)279 NameValuePair (org.apache.http.NameValuePair)191 ArrayList (java.util.ArrayList)178 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)96 HttpPost (org.apache.http.client.methods.HttpPost)81 HttpResponse (org.apache.http.HttpResponse)75 HttpEntity (org.apache.http.HttpEntity)57 IOException (java.io.IOException)51 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)33 Test (org.junit.Test)31 HttpGet (org.apache.http.client.methods.HttpGet)29 ClientProtocolException (org.apache.http.client.ClientProtocolException)28 CSVReader (com.talend.csv.CSVReader)27 HttpClient (org.apache.http.client.HttpClient)24 JSONObject (org.json.JSONObject)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)19 HashMap (java.util.HashMap)19 Map (java.util.Map)17 URI (java.net.URI)16 WebserviceInvocation (com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation)15