Search in sources :

Example 86 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project webmagic by code4craft.

the class HttpClientDownloader method addFormParams.

private RequestBuilder addFormParams(RequestBuilder requestBuilder, NameValuePair[] nameValuePair, Map<String, String> params) {
    List<NameValuePair> allNameValuePair = new ArrayList<NameValuePair>();
    if (nameValuePair != null && nameValuePair.length > 0) {
        allNameValuePair = Arrays.asList(nameValuePair);
    }
    if (params != null) {
        for (String key : params.keySet()) {
            allNameValuePair.add(new BasicNameValuePair(key, params.get(key)));
        }
    }
    requestBuilder.setEntity(new UrlEncodedFormEntity(allNameValuePair, Charset.forName("utf8")));
    return requestBuilder;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 87 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project PneumaticCraft by MineMaarten.

the class PastebinHandler method putInternal.

public String putInternal(String contents) {
    HttpPost httppost = new HttpPost("http://pastebin.com/api/api_post.php");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("api_dev_key", DEV_KEY));
    params.add(new BasicNameValuePair("api_paste_code", contents));
    params.add(new BasicNameValuePair("api_option", "paste"));
    if (isLoggedIn())
        params.add(new BasicNameValuePair("api_user_key", userKey));
    try {
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            return IOUtils.toString(instream, "UTF-8");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException)

Example 88 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project carat by amplab.

the class JsonParser method getJSONFromUrl.

public String getJSONFromUrl(String url) {
    InputStream inputStream = null;
    String result = null;
    ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
    try {
        // Set up HTTP post
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(param));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        // Read content & Log
        inputStream = httpEntity.getContent();
    } catch (UnknownHostException e0) {
        Log.d("JsonParser", "Unable to connect to the statstics server (no Internet on the device! is Wifi or mobile data on?), " + e0.toString());
        return "";
    } catch (UnsupportedEncodingException e1) {
        Log.e("UnsupportedEncodingException", e1.toString());
        return "";
    } catch (ClientProtocolException e2) {
        Log.e("ClientProtocolException", e2.toString());
        return "";
    } catch (IllegalStateException e3) {
        Log.e("IllegalStateException", e3.toString());
        return "";
    } catch (IOException e4) {
        Log.e("IOException", e4.toString());
        return "";
    }
    // Convert response to string using String Builder
    try {
        BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
        StringBuilder sBuilder = new StringBuilder();
        String line = null;
        while ((line = bReader.readLine()) != null) {
            sBuilder.append(line + "\n");
        }
        inputStream.close();
        result = sBuilder.toString();
    } catch (Exception e) {
        Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
    }
    return result;
}
Also used : NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) UnknownHostException(java.net.UnknownHostException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader)

Example 89 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project ninja by ninjaframework.

the class NinjaTestBrowser method makePostRequestWithFormParameters.

public String makePostRequestWithFormParameters(String url, Map<String, String> headers, Map<String, String> formParameters) {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = null;
    try {
        HttpPost postRequest = new HttpPost(url);
        if (headers != null) {
            // add all headers
            for (Entry<String, String> header : headers.entrySet()) {
                postRequest.addHeader(header.getKey(), header.getValue());
            }
        }
        // add form parameters:
        List<BasicNameValuePair> formparams = new ArrayList<>();
        if (formParameters != null) {
            for (Entry<String, String> parameter : formParameters.entrySet()) {
                formparams.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
            }
        }
        // encode form parameters and add
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams);
        postRequest.setEntity(entity);
        HttpResponse response;
        response = httpClient.execute(postRequest);
        br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String output;
        while ((output = br.readLine()) != null) {
            sb.append(output);
        }
        postRequest.releaseConnection();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOG.error("Failed to close resource", e);
            }
        }
    }
    return sb.toString();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) IOException(java.io.IOException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BufferedReader(java.io.BufferedReader)

Example 90 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project AndEngine by nicolasgramlich.

the class LevelStatsDBConnector method submitAsync.

public void submitAsync(final int pLevelID, final boolean pSolved, final int pSecondsElapsed, final Callback<Boolean> pCallback) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                /* Create a new HttpClient and Post Header. */
                final HttpClient httpClient = new DefaultHttpClient();
                final HttpPost httpPost = new HttpPost(LevelStatsDBConnector.this.mSubmitURL);
                /* Append POST data. */
                final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                nameValuePairs.add(new BasicNameValuePair("level_id", String.valueOf(pLevelID)));
                nameValuePairs.add(new BasicNameValuePair("solved", (pSolved) ? "1" : "0"));
                nameValuePairs.add(new BasicNameValuePair("secondsplayed", String.valueOf(pSecondsElapsed)));
                nameValuePairs.add(new BasicNameValuePair("player_id", String.valueOf(LevelStatsDBConnector.this.mPlayerID)));
                nameValuePairs.add(new BasicNameValuePair("secret", String.valueOf(LevelStatsDBConnector.this.mSecret)));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                /* Execute HTTP Post Request. */
                final HttpResponse httpResponse = httpClient.execute(httpPost);
                final int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    final String response = StreamUtils.readFully(httpResponse.getEntity().getContent());
                    if (response.equals("<success/>")) {
                        if (pCallback != null) {
                            pCallback.onCallback(true);
                        }
                    } else {
                        if (pCallback != null) {
                            pCallback.onCallback(false);
                        }
                    }
                } else {
                    if (pCallback != null) {
                        pCallback.onCallback(false);
                    }
                }
            } catch (final IOException e) {
                Debug.e(e);
                if (pCallback != null) {
                    pCallback.onCallback(false);
                }
            }
        }
    }).start();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)134 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)111 HttpPost (org.apache.http.client.methods.HttpPost)105 NameValuePair (org.apache.http.NameValuePair)100 ArrayList (java.util.ArrayList)96 HttpResponse (org.apache.http.HttpResponse)74 IOException (java.io.IOException)47 HttpEntity (org.apache.http.HttpEntity)40 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)31 ClientProtocolException (org.apache.http.client.ClientProtocolException)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)26 HttpClient (org.apache.http.client.HttpClient)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Map (java.util.Map)18 JSONObject (org.json.JSONObject)18 TestHttpClient (io.undertow.testutils.TestHttpClient)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashMap (java.util.HashMap)13 Header (org.apache.http.Header)13