Search in sources :

Example 96 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project mobile-android by photo.

the class ApiBase method createHttpRequest.

/**
     * Create a HttpUriRequest out of a ApiRequest object.
     * 
     * @param request the ApiRequest for which a HttpUriRequest should be
     *            created
     * @param baseUrl the base server url
     * @param listener Progress Listener with callback on progress
     * @return HttpUriRequest object which will do the request as described in
     *         ApiRequest
     * @throws UnsupportedEncodingException
     */
private HttpUriRequest createHttpRequest(ApiRequest request, String baseUrl, ProgressListener listener) throws UnsupportedEncodingException {
    HttpUriRequest httpRequest = null;
    switch(request.getMethod()) {
        case ApiRequest.GET:
            httpRequest = new HttpGet(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
        case ApiRequest.POST:
            httpRequest = new HttpPost(baseUrl + request.getPath());
            HttpPost httpPost = ((HttpPost) httpRequest);
            if (request.isMime()) {
                // TODO use the multipart when possible (currently server
                // handles it wrong)
                // HttpEntity entity = createMultipartEntity(request);
                // TODO remove this when doing correct multipart
                httpRequest = new HttpPost(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
                httpPost = ((HttpPost) httpRequest);
                HttpEntity entity = createFileOnlyMultipartEntity(request);
                if (listener != null) {
                    httpPost.setEntity(new HttpEntityWithProgress(entity, listener, httpPost));
                } else {
                    httpPost.setEntity(entity);
                }
            } else {
                httpPost.setEntity(new UrlEncodedFormEntity(request.getParameters(), HTTP.UTF_8));
            }
            break;
        case ApiRequest.PUT:
            httpRequest = new HttpPut(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
        case ApiRequest.DELETE:
            httpRequest = new HttpDelete(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
    }
    for (NameValuePair pair : request.getHeaders()) {
        request.addHeader(pair.getName(), pair.getValue());
    }
    return httpRequest;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HttpPut(org.apache.http.client.methods.HttpPut)

Example 97 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 98 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project pinpoint by naver.

the class ClosableAsyncHttpClientIT method test.

@Test
public void test() throws Exception {
    CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().useSystemProperties().build();
    httpClient.start();
    try {
        HttpPost httpRequest = new HttpPost("http://www.naver.com/");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("param1", "value1"));
        httpRequest.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8.name()));
        Future<HttpResponse> responseFuture = httpClient.execute(httpRequest, null);
        HttpResponse response = (HttpResponse) responseFuture.get();
        if ((response != null) && (response.getEntity() != null)) {
            EntityUtils.consume(response.getEntity());
        }
    } finally {
        httpClient.close();
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", CloseableHttpAsyncClient.class.getMethod("execute", HttpUriRequest.class, FutureCallback.class)));
    verifier.verifyTrace(async(event("HTTP_CLIENT_4", Class.forName("org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl").getMethod("start"), null, null, "www.naver.com", annotation("http.url", "http://www.naver.com/"), annotation("http.entity", "param1=value1")), event("ASYNC", "Asynchronous Invocation"), event("HTTP_CLIENT_4_INTERNAL", BasicFuture.class.getMethod("completed", Object.class))));
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", BasicFuture.class.getMethod("get")));
    verifier.verifyTraceCount(0);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicFuture(org.apache.http.concurrent.BasicFuture) ArrayList(java.util.ArrayList) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 99 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project pinpoint by naver.

the class HttpRemoteService method createPost.

private HttpPost createPost(String url, MultiValueMap<String, String> params) throws UnsupportedEncodingException {
    HttpPost post = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    for (Map.Entry<String, List<String>> entry : params.entrySet()) {
        String key = entry.getKey();
        for (String value : entry.getValue()) {
            nvps.add(new BasicNameValuePair(key, value));
        }
    }
    post.setEntity(new UrlEncodedFormEntity(nvps));
    return post;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 100 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project DrupalCloud by INsReady.

the class JSONServerClient method call.

/**
	 * Generic request
	 * 
	 * @param method
	 *            Request name
	 * @param parameters
	 *            Parameters
	 * @return result string
	 */
public String call(String method, BasicNameValuePair[] parameters) throws ServiceNotAvailableException {
    String sessid = this.getSessionID();
    mPairs.clear();
    String nonce = Integer.toString(new Random().nextInt());
    Mac hmac;
    try {
        hmac = Mac.getInstance(JSONServerClient.mALGORITHM);
        final Long timestamp = new Date().getTime() / 100;
        final String time = timestamp.toString();
        hmac.init(new SecretKeySpec(JSONServerClient.mAPI_KEY.getBytes(), JSONServerClient.mALGORITHM));
        String message = time + ";" + JSONServerClient.mDOMAIN + ";" + nonce + ";" + method;
        hmac.update(message.getBytes());
        String hmac_value = new String(Hex.encodeHex(hmac.doFinal()));
        mPairs.add(new BasicNameValuePair("hash", hmac_value));
        mPairs.add(new BasicNameValuePair("domain_name", JSONServerClient.mDOMAIN));
        mPairs.add(new BasicNameValuePair("domain_time_stamp", time));
        mPairs.add(new BasicNameValuePair("nonce", nonce));
        mPairs.add(new BasicNameValuePair("method", method));
        mPairs.add(new BasicNameValuePair("api_key", JSONServerClient.mAPI_KEY));
        mPairs.add(new BasicNameValuePair("sessid", sessid));
        for (int i = 0; i < parameters.length; i++) {
            mPairs.add(parameters[i]);
        }
        mSERVER.setEntity(new UrlEncodedFormEntity(mPairs));
        HttpResponse response = mClient.execute(mSERVER);
        InputStream is = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String result = br.readLine();
        JSONObject jso;
        jso = new JSONObject(result);
        boolean error = jso.getBoolean("#error");
        if (error) {
            String errorMsg = jso.getString("#data");
            throw new ServiceNotAvailableException(this, errorMsg);
        }
        return result;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
        throw new ServiceNotAvailableException("Remote server is not available");
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) Date(java.util.Date) ClientProtocolException(org.apache.http.client.ClientProtocolException) Random(java.util.Random) JSONObject(org.json.JSONObject) SecretKeySpec(javax.crypto.spec.SecretKeySpec) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BufferedReader(java.io.BufferedReader)

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)127 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)105 HttpPost (org.apache.http.client.methods.HttpPost)99 NameValuePair (org.apache.http.NameValuePair)94 ArrayList (java.util.ArrayList)91 HttpResponse (org.apache.http.HttpResponse)71 IOException (java.io.IOException)45 HttpEntity (org.apache.http.HttpEntity)39 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)30 ClientProtocolException (org.apache.http.client.ClientProtocolException)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)23 Test (org.junit.Test)20 HttpClient (org.apache.http.client.HttpClient)19 HttpGet (org.apache.http.client.methods.HttpGet)19 JSONObject (org.json.JSONObject)17 Map (java.util.Map)16 TestHttpClient (io.undertow.testutils.TestHttpClient)14 Header (org.apache.http.Header)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 HashMap (java.util.HashMap)12