Example usage for org.apache.http.client.methods HttpPost HttpPost

List of usage examples for org.apache.http.client.methods HttpPost HttpPost

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost HttpPost.

Prototype

public HttpPost(final String uri) 

Source Link

Usage

From source file:fr.xebia.workshop.android.core.utils.ServerUtils.java

private static HttpPost buildRegistationIdRequest(String registrationId, String deviceId, Long userId)
        throws JSONException, UnsupportedEncodingException {
    HttpPost putRequest = new HttpPost(GCM_REGISTRATION_URL);
    putRequest.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    JSONObject regObject = new JSONObject();
    regObject.put("deviceId", deviceId);
    regObject.put("registrationId", registrationId);
    JSONObject userObject = new JSONObject();
    userObject.put("id", userId);
    regObject.put("user", userObject);
    putRequest.setEntity(new StringEntity(regObject.toString()));
    return putRequest;
}

From source file:com.rogiel.httpchannel.http.PostRequest.java

@Override
public HttpResponse request() throws IOException {
    final HttpPost post = new HttpPost(uri);
    for (final Header header : headers) {
        post.addHeader(header);//from  ww w  .  j av  a 2s .c  om
    }
    post.setEntity(new UrlEncodedFormEntity(params));
    return ctx.client.execute(post);
}

From source file:es.logongas.iothome.agent.http.Http.java

public Object post(URL url, Object data) {
    try {/*from ww  w .  j  a v a2 s.c  o m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(url.toURI());

        StringEntity input = new StringEntity(objectMapper.writeValueAsString(data));
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);
        String output = inputStreamToString(response.getEntity().getContent());

        if (response.getStatusLine().getStatusCode() != 201) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + "\n" + output);
        }

        httpClient.getConnectionManager().shutdown();

        return objectMapper.readValue(output, data.getClass());

    } catch (Exception ex) {

        throw new RuntimeException(ex);

    }
}

From source file:de.hska.ld.core.client.PostClientRequest.java

public void execute(HttpEntity entity, String accessToken) throws RequestStatusNotOKException {
    HttpPost post = new HttpPost(this.url);
    if (accessToken != null) {
        addHeaderInformation(post, accessToken);
    }/*from  w  w  w  . j a  v a2  s.  co  m*/
    if (entity != null) {
        post.setEntity(entity);
    }
    try {
        this.response = this.client.execute(post);
        if (entity instanceof StringEntity) {
            this.processResponse(((StringEntity) entity));
        } else {
            this.processResponse();
        }
    } catch (IOException e) {
        this.exceptionLogger.log(this.getLoggingPrefix() + this.action, e);
    }
}

From source file:ec.edu.ucuenca.dcc.sld.HttpUtils.java

public static String sendPost2(String operation, String body) throws Exception {
    //   System.out.println ("body "+body);
    String url = "http://api.cortical.io/rest/" + operation + "?retina_name=en_associative";

    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);
    post.setHeader("api-key", "0e30f600-7b62-11e6-a057-97f4c970893c");
    post.setHeader("content-type", "application/json");
    //   post.setHeader("api-key","0e30f600-7b62-11e6-a057-97f4c970893c");
    //post.setHeader("charset", "utf-8");
    StringEntity params = new StringEntity(body, Charset.defaultCharset());
    //List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    //urlParameters.add(new BasicNameValuePair("body", body));
    //urlParameters.add(new BasicNameValuePair("retina_name","en_associative"));
    /*urlParameters.add(new BasicNameValuePair("locale", ""));
    urlParameters.add(new BasicNameValuePair("caller", ""));
    urlParameters.add(new BasicNameValuePair("num", "12345"));*/

    post.setEntity(params);//from www. j  a va  2 s .co m
    //  post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    // System.out.println("\nSending 'POST' request to URL : " + url);
    // System.out.println("Post parameters : " + post.getEntity());
    // System.out.println("Response Code : "
    //     + response.getStatusLine().getStatusCode());

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    //  System.out.println(result.toString());
    int code = response.getStatusLine().getStatusCode();
    if (400 == code) {
        return "{\"overlappingAll\":0 , \"weightedScoring\": 0 } ";
    } else {
        return result.toString();
    }
}

From source file:com.dss886.nForumSDK.http.PostMethod.java

public PostMethod(DefaultHttpClient httpClient, String auth, String url, ParamOption params) {
    this.httpClient = httpClient;
    httpPost = new HttpPost(url);
    try {//from   ww  w.  j  a va  2s.  c  om
        httpPost.setEntity(new UrlEncodedFormEntity(params.toNamePair(), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    httpPost.setHeader("Accept-Encoding", "gzip, deflate");
    httpPost.setHeader("Authorization", "Basic " + auth);
}

From source file:com.grinnellplans.plandroid.PlanFetchTask.java

protected String doInBackground(String... params) {
    Log.i("PlanFetchTask::doInBackground", "Started");
    AndroidHttpClient plansClient = AndroidHttpClient.newInstance("plandroid");

    final HttpPost req = new HttpPost("http://" + _ss.get_serverName() + "/api/1/index.php?task=read");
    List<NameValuePair> postParams = new ArrayList<NameValuePair>(1);
    postParams.add(new BasicNameValuePair("username", params[0]));
    postParams.add(new BasicNameValuePair("readlinkreplacement", "/plan/{username}"));
    Log.i("PlanFetchTask::doInBackground", "setting postParams");
    try {//from  w  w w.  ja v  a  2 s  . c  om
        req.setEntity(new UrlEncodedFormEntity(postParams));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    HttpResponse response = null;
    try {
        Log.i("PlanFetchTask::doInBackground", "executing request");
        response = plansClient.execute(req, _httpContext);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String resp = null;
    try {
        Log.i("PlanFetchTask::doInBackground", "reading response");
        resp = new BufferedReader((new InputStreamReader(response.getEntity().getContent()))).readLine();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    plansClient.close();
    Log.i("PlanFetchTask::doInBackground", "server responded \"" + resp + "\"");
    return resp;
}

From source file:com.psbk.modulperwalian.Controller.ControllerLogin.java

public JSONObject getDataLogin() {
    JSONObject obj = null;// w w w. j a  v a 2 s .  c  o m
    HttpClient httpClient = HttpClientBuilder.create().build();
    String status = null;
    try {
        String url = getIp() + "login";
        HttpPost request = new HttpPost(url);
        StringEntity params = new StringEntity("{request:{\"username\":\"" + getUsername() + "\","
                + "\"password\":\"" + getPassword() + "\" } }");
        request.addHeader("content-type", "application/json");
        request.setEntity(params);

        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        status = EntityUtils.toString(entity, "UTF-8");
        JSONObject result;
        obj = new JSONObject(status);

    } catch (Exception ex) {
        System.out.println("Error karena : " + ex.getMessage());
    }

    return obj;
}

From source file:com.devbliss.doctest.httpfactory.PostUploadWithoutRedirectImpl.java

public HttpPost createPostRequest(URI uri, Object payload) throws IOException {
    HttpPost httpPost = new HttpPost(uri);
    HttpParams params = new BasicHttpParams();
    params.setParameter(HttpConstants.HANDLE_REDIRECTS, false);
    httpPost.setParams(params);/*from w ww. j  a v a 2  s.c o  m*/

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart(paramName, fileBodyToUpload);
    httpPost.setEntity(entity);

    return httpPost;
}

From source file:att.jaxrs.client.Content_tag.java

public static String addContent_tag(long content_id, long tag_id) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", Long.toString(content_id)));
    urlParameters.add(new BasicNameValuePair("tag_id", Long.toString(tag_id)));

    String resultStr = "";

    try {//from   ww  w.j  a  v  a2s .c  om

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.INSERT_CONTENT_TAG_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result.getEntity().getContent());
        System.out.println(Constants.RESPONSE_BODY + resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}