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:vn.vndirect.form.model.SendURIModel.java

public SendURIModel(String image, String filenameKH, String filenameKN) {
    try {/*from w ww . j  av a2s.  c  om*/
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(URItoGMC + "?ImgName=" + image + "&csvKH=" + "KH" + filenameKH + ".csv"
                + "&csvKN=" + "KN" + filenameKN + ".csv");
        post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + "UTF-16");
        //post.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response = httpclient.execute(post);
        System.out.println("Header " + response.getFirstHeader("Location"));
    } catch (Exception e) {
    }

}

From source file:pj.rozkladWKD.HttpClient.java

public static JSONObject SendHttpPost(List<NameValuePair> nameValuePairs)
        throws JSONException, ClientProtocolException, IOException {

    // Create a new HttpClient and Post Header  
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.mmaj.nazwa.pl/rozkladwkd/listener2.php");
    nameValuePairs.add(new BasicNameValuePair("app_ver", RozkladWKD.APP_VERSION));

    // Add your data  
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF_8"));

    // Execute HTTP Post Request  
    HttpResponse response = httpclient.execute(httppost);

    HttpEntity entity = response.getEntity();

    if (entity != null) {
        // Read the content stream

        InputStream instream = entity.getContent();
        Header contentEncoding = response.getFirstHeader("Content-Encoding");
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            instream = new GZIPInputStream(instream);
        }/* w w w .j a  va2s  .  co  m*/

        // convert content stream to a String
        String resultString = convertStreamToString(instream);

        instream.close();
        //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

        // Transform the String into a JSONObject
        if (RozkladWKD.DEBUG_LOG) {
            Log.i(TAG, "result: " + resultString);
        }
        JSONObject jsonObjRecv = new JSONObject(resultString);
        // Raw DEBUG output of our received JSON object:
        if (RozkladWKD.DEBUG_LOG) {
            Log.i(TAG, "<jsonobject>\n" + jsonObjRecv.toString() + "\n</jsonobject>");
        }

        return jsonObjRecv;
    }

    return null;
}

From source file:WSpatern.uploadFile.java

public void Uploadfile(String token, String dirid, String userid, byte[] file) {
    try {// w  w w  .  j  ava  2s  . c o  m
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("https://documenta-dms.com/DMSWS/api/v1/file/upload/" + token + "/" + dirid
                + "/" + userid + "/" + Arrays.toString(file));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {

            System.out.println(line);
            // parseXML(line);

        }
    } catch (IOException ex) {
        Logger.getLogger(uploadFile.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.github.rnewson.couchdb.lucene.couchdb.HttpUtils.java

public static final String post(final HttpClient httpClient, final String url, final JSONObject body)
        throws IOException {
    final HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", Constants.APPLICATION_JSON);
    post.setEntity(new StringEntity(body.toString(), "UTF-8"));
    return execute(httpClient, post);
}

From source file:com.core.ServerConnector.java

public static String post(String endpoint, Map<String, String> params) throws Exception {
    String result = null;/*w ww .  ja v a  2s . co m*/
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);
    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    HttpPost httppost = new HttpPost(endpoint);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        nameValuePairs.add(new BasicNameValuePair(param.getKey(), param.getValue()));
    }
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        result = SystemUtil.convertStreamToString(instream);

        instream.close();
    }

    return result;
}

From source file:edu.illinois.whereru.HTTPRequest.java

public static JSONObject makeHttpRequest(String urlString, String method, List<NameValuePair> params) {

    JSONObject jsonObject = null;/*from w w  w.  jav  a2  s. co  m*/

    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse;

        if (method.equals("POST")) {
            // init post
            HttpPost httpPost = new HttpPost(urlString);
            // set the resource to send
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            // send the request, retrieve response
            httpResponse = httpClient.execute(httpPost);

        } else {
            // GET method
            // formulate url
            String paramString = URLEncodedUtils.format(params, "utf-8");
            urlString += "?" + paramString;
            // init GET
            HttpGet httpGet = new HttpGet(urlString);
            // send the request, retrieve response
            httpResponse = httpClient.execute(httpGet);

        }

        // retrieve content from the response
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream is = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        jsonObject = new JSONObject(sb.toString());
    } catch (NullPointerException e) {

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}

From source file:org.graylog2.systeminformation.Sender.java

public static void send(Map<String, Object> info, Core server) {
    HttpClient httpClient = new DefaultHttpClient();

    try {/*from   ww  w.j a  v a  2  s .  c  om*/
        HttpPost request = new HttpPost(getTarget(server));

        List<NameValuePair> nameValuePairs = Lists.newArrayList();
        nameValuePairs.add(new BasicNameValuePair("systeminfo", JSONValue.toJSONString(info)));
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpClient.execute(request);

        if (response.getStatusLine().getStatusCode() != 201) {
            LOG.warn("Response code for system statistics was not 201, but "
                    + response.getStatusLine().getStatusCode());
        }
    } catch (Exception e) {
        LOG.warn("Could not send system statistics.", e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:es.ugr.swad.swadroid.webservices.RestEasy.java

public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");

    request.setEntity(s);/*from w  w w. j  a  v  a  2 s.co  m*/
    request.addHeader("accept", "application/json");

    return httpclient.execute(request);
}

From source file:eu.ebbitsproject.peoplemanager.utils.HttpUtils.java

public static void postData(String url, String action, String errorType, String objectId, String personId,
        String eventId) {//ww  w. j a v  a 2 s . c om
    CloseableHttpClient httpClient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("action", action));
    nvps.add(new BasicNameValuePair("personId", personId));
    nvps.add(new BasicNameValuePair("errorType", errorType));
    nvps.add(new BasicNameValuePair("objectId", objectId));
    nvps.add(new BasicNameValuePair("eventId", eventId));

    System.out.println("action = " + action);
    System.out.println("personId = " + personId);
    System.out.println("errorType = " + errorType);
    System.out.println("objectId = " + objectId);
    System.out.println("eventId = " + eventId);

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, ex);
    }

    CloseableHttpResponse response = null;
    try {
        response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    } catch (IOException e) {
        Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        try {
            response.close();
        } catch (IOException e) {
            Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}

From source file:CB_Core.Api.CB_Api.java

/**
 * Gibt die bei Team-Cachebox.de hinterlegte GC Auth url zurck
 * //w  w  w . j a v  a2 s. c om
 * @param staging
 *            Config.settings.StagingAPI.getValue()
 * @return String
 */
public static String getGcAuthUrl() {
    String result = "";

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                CB_Core_Settings.StagingAPI.getValue() ? CB_API_URL_GET_URLS_Staging : CB_API_URL_GET_URLS);

        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Content-type", "application/json");

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            result += line + "\n";
        }

        try
        // Parse JSON Result
        {
            JSONTokener tokener = new JSONTokener(result);
            JSONObject json = (JSONObject) tokener.nextValue();
            if (CB_Core_Settings.StagingAPI.getValue())
                return json.getString("GcAuth_ACB_Staging");
            return json.getString("GcAuth_ACB");

        } catch (JSONException e) {
            e.printStackTrace();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
    return "";
}