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:com.socioffice.grabmenu.model.JSONRequest.java

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {//  w  ww .j av  a  2 s. co m
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");

        // only set this parameter if you would like to use gzip compression
        httpPostRequest.setHeader("Accept-Encoding", "gzip");

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        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);
            }

            // 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
            JSONObject jsonObjRecv = new JSONObject(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<jsonobject>\n" + jsonObjRecv.toString() + "\n</jsonobject>");

            return jsonObjRecv;
        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

From source file:ca.dal.cs.csci4126.quizboard.library.HttpClient.java

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {//ww  w .ja  va  2  s.c om
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        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);
            }

            // 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
            JSONObject jsonObjRecv = new JSONObject(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");

            return jsonObjRecv;
        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

From source file:com.example.montxu.magik_repair.HttpClient.java

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {/*from w  ww .  j a va  2s .  c  o  m*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        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);
            }

            // 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
            JSONObject jsonObjRecv = new JSONObject(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");

            return jsonObjRecv;
        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.

        e.printStackTrace();
    }

    return null;
}

From source file:app.wz.HttpClient.java

public static String SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {/*  w ww  . ja  va  2s . co m*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            return EntityUtils.toString(entity);
            //            // 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);
            //            }
            //
            //            // 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
            //            JSONObject jsonObjRecv = new JSONObject(resultString);
            //            // Raw DEBUG output of our received JSON object:
            //            Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
            //
            //            return jsonObjRecv;
        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

From source file:org.mumod.util.MustardUtil.java

public static void snapshot(Context context, String id, String version, String accountNumber) {
    try {// w  w w.  j a v  a  2s  .c o  m
        HttpPost post = new HttpPost("http://mustard.macno.org/snapshot.php");
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("v", version));
        params.add(new BasicNameValuePair("n", accountNumber));
        params.add(new BasicNameValuePair("m", id));
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpManager hm = new HttpManager(context);
        DefaultHttpClient hc = hm.getHttpClient();
        hc.execute(post);
    } catch (Exception e) {
    }
}

From source file:com.kingmed.dp.aperio.DsClient.java

public static String logon() throws Exception {
    String token = null;/*w w  w  . jav  a 2  s .c  o m*/
    String url = "http://192.168.180.132:86/Aperio.Security/Security2.asmx";
    String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://www.aperio.com/webservices/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <SOAP-ENV:Body> <Logon><Token>leQJYfWQ6wv_tJa6hhBZlWwgrRZ-mDywnfb9F4EfC1752Pt07NZDEGvFNYYPvpxkN0IvPTrPi0M=</Token><LoginName>gzuser</LoginName><Password>gzking</Password></Logon></SOAP-ENV:Body> </SOAP-ENV:Envelope> ";

    HttpClient hc = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Content-Type", "text/xml;charset=utf-8");
    httpPost.addHeader("SOAPAction", "http://www.aperio.com/webservices/#Logon");
    StringEntity myEntity = new StringEntity(body, ContentType.create("text/xml", "UTF-8"));
    httpPost.setEntity(myEntity);
    System.out.println(myEntity.getContentType());
    System.out.println("Content-Length" + myEntity.getContentLength());
    HttpResponse res = null;
    res = (CloseableHttpResponse) hc.execute(httpPost);
    HttpEntity entity = res.getEntity();
    System.out.println(EntityUtils.toString(entity));

    return token;
}

From source file:org.opencastproject.remotetest.server.resource.DistributeResources.java

/**
 * /*  ww w  .  j  ava2s  .c  o m*/
 * @param channel
 *          Distribution channel: local, youtube, itunesu
 * 
 */
public static HttpResponse distribute(TrustedHttpClient client, String channel, String mediapackage,
        String... elementId) throws Exception {
    HttpPost post = new HttpPost(getServiceUrl() + channel.toLowerCase() + "/");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("mediapackage", mediapackage));
    for (String id : elementId) {
        params.add(new BasicNameValuePair("elementId", id));
    }
    post.setEntity(new UrlEncodedFormEntity(params));

    return client.execute(post);
}

From source file:com.ilearnrw.reader.utils.HttpHelper.java

public static HttpResponse post(String url, String data) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = null;//w  ww  .j  a  v a  2  s .  co  m

    //HttpConnectionParams.setSoTimeout(client.getParams(), 25000);

    HttpPost post = new HttpPost(url);
    post.setHeader("Accept", "application/json");
    post.setHeader("Authorization", "Basic " + authString);
    post.setHeader("Content-Type", "application/json;charset=utf-8");

    try {
        post.setEntity(new StringEntity(data, HTTP.UTF_8));
        response = client.execute(post);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return response;
}

From source file:de.siemens.quantarch.personService.PersonServiceClient.java

public static final long getPerson(String name, String email, long projectId, String personServiceUrl) {
    long userId = 0;
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(personServiceUrl + "/post_user_id");
    try {/*  www  .  ja  va  2s.  co m*/
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("projectID", Long.toString(projectId)));
        nameValuePairs.add(new BasicNameValuePair("name", name));
        nameValuePairs.add(new BasicNameValuePair("email", email));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        // read output
        StringBuilder builder = new StringBuilder();
        String line = null;
        while ((line = rd.readLine()) != null) {
            builder.append(line);
        }

        JSONObject json = JSONObject.fromObject(builder.toString());
        userId = json.getLong("id");
        return userId;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return userId;
}

From source file:org.mustard.util.MustardUtil.java

public static void snapshot(String id, String version, String accountNumber) {
    try {/*  www . j av a 2s . co  m*/
        HttpPost post = new HttpPost("http://mustard.macno.org/snapshot.php");
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("v", version));
        params.add(new BasicNameValuePair("n", accountNumber));
        params.add(new BasicNameValuePair("m", id));
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        DefaultHttpClient hc = new DefaultHttpClient();
        HttpProtocolParams.setUserAgent(post.getParams(), "Mustard/1.0");
        HttpProtocolParams.setUseExpectContinue(post.getParams(), false);
        hc.execute(post);
    } catch (Exception e) {
    }
}