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:Main.java

public static String getRepsonseString(String url, Map<String, String> params) {
    HttpPost request = new HttpPost(url);
    List<NameValuePair> p = new ArrayList<NameValuePair>();
    for (String key : params.keySet()) {
        p.add(new BasicNameValuePair(key, params.get(key)));
    }//w  w w  . j  ava 2 s  . co  m
    HttpClient httpClient = new DefaultHttpClient();
    String result = null;

    try {
        request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == 200) {
            result = new String(EntityUtils.toString(response.getEntity()).getBytes("ISO_8859_1"), "UTF-8");
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (httpClient != null)
            httpClient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:Main.java

public static String getJSONFromUrlWithPostRequest(String url, List<NameValuePair> params) {

    try {/*from   w  ww.j a  va  2 s. co m*/
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        String tempResponse = EntityUtils.toString(httpResponse.getEntity());

        return tempResponse;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

public static String makeRequest1(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {/*from www . j  a  v  a2 s.  c  o m*/
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));

        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.v(TAG, "output-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String makeRequest3(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {/*from   ww w .  ja  va 2  s  . c  om*/
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));
        //httpPost.setHeader("sessionToken",AuthToken);
        // httpPost.setHeader("Accept", "application/json");
        //  httpPost.setHeader("Content-type", "application/json"); //text/html
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.d("tag", "outputtttttttttt-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String makeRequest55(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {//  ww  w .j a  v a  2 s .  c o m
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        //text/html
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.v(TAG, "output-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String makeRequest(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {/*ww  w.  j  a  va 2s  .c  o  m*/
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("sessionToken", "61");
        //text/html
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.v(TAG, "output-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String openPostConnection(String urlString, Map<String, String> map) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(urlString);
    List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (String key : map.keySet()) {
        nameValuePairs.add(new BasicNameValuePair(key, map.get(key)));
    }//from  w w w.  ja v a 2  s .  co m
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    try {
        HttpResponse httpResponse = client.execute(post);
        BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        StringBuilder json = new StringBuilder("");
        String line = null;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }
        return json.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Makes a POST call to the server./*from w ww .  j  ava2  s  . co m*/
 * @param params 0-> Call Method = POST; 1-> URL; ... -> Params
 * @return
 */
public static String httpPost(String... params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(params[1]);

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        for (int i = 1; i + 1 < params.length; i += 2) {
            nameValuePairs.add(new BasicNameValuePair(params[i], params[i + 1]));
        }
        if (!nameValuePairs.isEmpty()) {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        System.out.println(response.toString());
        return response.toString();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return "Client Protocol Exception";
    } catch (IOException e) {
        e.printStackTrace();
        return "POST: Failed to connect (" + params[1] + ")";
    }
}

From source file:com.mycompany.trader.HTTPTransport.java

public static HttpPost createPost(String resource, String data) {
    try {// w  ww .ja  va 2s  .c  o  m
        HttpPost post = new HttpPost(url + resource);
        StringEntity input = new StringEntity(data);
        input.setContentType("application/json");
        post.setEntity(input);
        return post;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getStringFromUrl(List<NameValuePair> nameValuePairs)
        throws ClientProtocolException, IOException {
    String url = "http://www.fsurugby.org/serve/request.php";
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = client.execute(httppost);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity buffer = new BufferedHttpEntity(entity);
    InputStream is = buffer.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line;//  ww  w.ja  v  a 2  s.  co  m
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    return sb.toString();
}