Example usage for org.apache.http.client.fluent Request bodyForm

List of usage examples for org.apache.http.client.fluent Request bodyForm

Introduction

In this page you can find the example usage for org.apache.http.client.fluent Request bodyForm.

Prototype

public Request bodyForm(final NameValuePair... formParams) 

Source Link

Usage

From source file:com.mywork.framework.util.RemoteHttpUtil.java

/**
 * ?getpost????/*from  w  ww  .  j  a  va  2  s.c om*/
 */
public static String fetchSimpleHttpResponse(String method, String contentUrl, Map<String, String> headerMap,
        Map<String, String> bodyMap) throws IOException {
    Executor executor = Executor.newInstance(httpClient);
    if (HttpGet.METHOD_NAME.equalsIgnoreCase(method)) {
        String result = contentUrl;
        StringBuilder sb = new StringBuilder();
        sb.append(contentUrl);
        if (bodyMap != null && !bodyMap.isEmpty()) {
            if (contentUrl.indexOf("?") > 0) {
                sb.append("&");
            } else {
                sb.append("?");
            }
            result = Joiner.on("&").appendTo(sb, bodyMap.entrySet()).toString();
        }

        return executor.execute(Request.Get(result)).returnContent().asString();
    }
    if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)) {
        Request request = Request.Post(contentUrl);
        if (headerMap != null && !headerMap.isEmpty()) {
            for (Map.Entry<String, String> m : headerMap.entrySet()) {
                request.addHeader(m.getKey(), m.getValue());
            }
        }
        if (bodyMap != null && !bodyMap.isEmpty()) {
            Form form = Form.form();
            for (Map.Entry<String, String> m : bodyMap.entrySet()) {
                form.add(m.getKey(), m.getValue());
            }
            request.bodyForm(form.build());
        }
        return executor.execute(request).returnContent().asString();
    }
    return null;

}

From source file:org.vas.test.rest.RestImpl.java

@Override
public <T> Response<T> put(String uri, Map<String, String> datas, Class<T> klass, Object... args) {
    uri = formattedUri(uri, args);/*  ww  w.  j  a v a 2 s  . co  m*/

    Request request = httpPut(uri);
    request.bodyForm(nameValuePairOf(datas));

    try {
        org.apache.http.client.fluent.Response response = request.execute();
        return extractResponse(klass, response);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.vas.test.rest.RestImpl.java

@Override
public <T> Response<T> post(String uri, Map<String, String> datas, Class<T> klass, Object... args) {
    uri = formattedUri(uri, args);/*from   ww w . ja va  2 s.c o  m*/

    Request request = httpPost(uri);
    request.bodyForm(nameValuePairOf(datas));

    try {
        org.apache.http.client.fluent.Response response = request.execute();
        return extractResponse(klass, response);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.jhuapl.dorset.http.apache.ApacheHttpClient.java

private Response put(HttpRequest request) throws IOException {
    Request apacheRequest = Request.Put(request.getUrl());
    if (request.getBody() != null) {
        ContentType ct = ContentType.create(request.getContentType().getMimeType(),
                request.getContentType().getCharset());
        apacheRequest.bodyString(request.getBody(), ct);
    } else if (request.getBodyForm() != null) {
        apacheRequest.bodyForm(buildFormBody(request.getBodyForm()));
    }//w  w  w .j  a v  a 2 s . c o m
    prepareRequest(apacheRequest);
    return apacheRequest.execute();
}

From source file:edu.jhuapl.dorset.http.apache.ApacheHttpClient.java

private Response post(HttpRequest request) throws IOException {
    Request apacheRequest = Request.Post(request.getUrl());
    if (request.getBody() != null) {
        ContentType ct = ContentType.create(request.getContentType().getMimeType(),
                request.getContentType().getCharset());
        apacheRequest.bodyString(request.getBody(), ct);
    } else if (request.getBodyForm() != null) {
        apacheRequest.bodyForm(buildFormBody(request.getBodyForm()));
    }/*from w  w  w . ja  v  a2 s . com*/
    prepareRequest(apacheRequest);
    return apacheRequest.execute();
}