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.sender.request.SenderUtil.java

public static void post() throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/ServicioWebUtil/ServicioUtil?wsdl");
    String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:util=\"http://util.servicio.com/\">\n"
            + "   <soapenv:Header/>\n" + "   <soapenv:Body>\n" + "      <util:saludar/>\n"
            + "   </soapenv:Body>\n" + "</soapenv:Envelope>";
    HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
    post.setEntity(entity);//from  ww w  .  jav a  2s  . co  m
    HttpResponse response = client.execute(post);
    String result = EntityUtils.toString(response.getEntity());

    System.out.println("Resultado: " + result);
}

From source file:Main.java

public static String postReqAsJson(String uri, String requestJson) throws ClientProtocolException, IOException {
    Log.i(TAG, "Send data to :" + uri + " ========== and the data str:" + requestJson);
    HttpPost post = new HttpPost(uri);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("attendanceClientJSON", requestJson));
    post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String retStr = EntityUtils.toString(response.getEntity());
        Log.i(TAG, "=================response str:" + retStr);
        return retStr;
    }//from www .j  a va2s .  c  om

    return response.getStatusLine().getStatusCode() + "ERROR";
}

From source file:Main.java

/**
 * Make POST request and return plain response
 * @param url// w ww .ja v  a2s.c  o  m
 * @param pairs
 * @return Plain text response
 * @throws Exception
 */
public static String makeHttpPostRequest(String url, List<NameValuePair> pairs) throws Exception {
    BufferedReader in = null;
    String result = "";
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);

        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        result = sb.toString();
        // System.out.println(result);
    } catch (Exception e) {

        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return result;
}

From source file:com.fufang.httprequest.HttpPostBuild.java

public static String postBuildJson(String url, String params)
        throws UnsupportedEncodingException, ClientProtocolException, IOException {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    String postResult = null;/* w ww  . j a  va2s . c o  m*/

    HttpPost httpPost = new HttpPost(url);
    System.out.println(" request " + httpPost.getRequestLine());

    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
            .build();
    httpPost.setConfig(requestConfig);
    StringEntity entity = new StringEntity(params.toString(), "UTF-8");
    entity.setContentType("application/json");
    httpPost.setEntity(entity);

    try {
        HttpResponse response = httpClient.execute(httpPost);
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity responseEntity = response.getEntity();
            postResult = EntityUtils.toString(responseEntity);
        } else {
            System.out.println("unexpected response status - " + status);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return postResult;
}

From source file:com.yattatech.gcm.gui.GCMSender.java

public static void send(String channel, String message, String classKey) throws Exception {

    loadProperties();/*from   w  w  w  .  j  av a2s  .c om*/
    final HttpClient httpClient = new DefaultHttpClient();
    final HttpPost httpPost = new HttpPost("https://android.googleapis.com/gcm/send");
    final Class<?> formatClass = Class.forName(PROPERTIES.getProperty(classKey));
    final RequestFormat format = (RequestFormat) formatClass.newInstance();
    //HttpHost proxy    = new HttpHost(PROPERTIES.getProperty("proxy.host"), Integer.parseInt(PROPERTIES.getProperty("proxy.port")));                  
    //httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);      
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Authorization", PROPERTIES.getProperty("auth.key"));
    httpPost.setEntity(format.getRequest(channel, message));
    HttpResponse response = httpClient.execute(httpPost);
    final String content = IOUtils.toString(response.getEntity().getContent());
    System.out.println("Response status " + response.getStatusLine().getStatusCode());
    if (logCallback != null) {
        logCallback.message(content);
    }
    System.out.println("Response " + content);
}

From source file:Extras.JSON.java

public static HttpResponse request(String URL, List parametros) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(URL);
    HttpResponse response = null;//from ww  w  . jav  a  2  s .c o  m
    try {
        httppost.setEntity(new UrlEncodedFormEntity(parametros));
        response = httpclient.execute(httppost);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return response;
}

From source file:Main.java

public static String postReqAsJsonAddParam(String uri, String requestJson, Map<String, String> param)
        throws ClientProtocolException, IOException {
    Log.i(TAG, "Send data to :" + uri + " ========== and the data str:" + requestJson);
    HttpPost post = new HttpPost(uri);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : param.entrySet()) {
        parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }//from   ww  w. j  a v  a 2  s  .  com
    parameters.add(new BasicNameValuePair("attendanceClientJSON", requestJson));
    post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String retStr = EntityUtils.toString(response.getEntity());
        Log.i(TAG, "=================response str:" + retStr);
        return retStr;
    }

    return response.getStatusLine().getStatusCode() + "ERROR";
}

From source file:Main.java

public static String getResultPost(String uri, List<NameValuePair> params) {
    String Result = null;//w ww  .j  a  va  2  s. co m
    try {
        if (uri == null) {
            return "";
        }
        HttpPost httpRequest = new HttpPost(uri);
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpResponse httpResponse = httpClient.execute(httpRequest);
        int res = httpResponse.getStatusLine().getStatusCode();
        if (res == 200) {

            StringBuilder builder = new StringBuilder();
            BufferedReader bufferedReader2 = new BufferedReader(
                    new InputStreamReader(httpResponse.getEntity().getContent()));
            for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine()) {
                builder.append(s);
            }
            Result = builder.toString();

        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
        return "";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Result;
}

From source file:Main.java

public static String initializeService(String data, String serviceName, String serviceUrl) {
    Log.d("EliademyUtils", "initializeService");
    try {//  www  . j a va2s  .  c  o m
        JSONObject jsObj = new JSONObject(data.toString());
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(serviceUrl + "/login/token.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", jsObj.get("username").toString()));
        nameValuePairs.add(new BasicNameValuePair("password", jsObj.get("password").toString()));
        nameValuePairs.add(new BasicNameValuePair("service", serviceName));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            StringBuilder jsonStr = new StringBuilder();
            InputStream iStream = entity.getContent();
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(iStream));
            String jpart;
            while ((jpart = bufferReader.readLine()) != null) {
                jsonStr.append(jpart);
            }
            iStream.close();
            Log.d("Moodle", jsonStr.toString());
            JSONObject jsonObj = new JSONObject(jsonStr.toString());
            return (String) jsonObj.get("token");
        }
    } catch (Exception e) {
        Log.e("EliademyUtils", "exception", e);
        return null;
    }
    return null;
}

From source file:de.nico.asura.tools.JSONParser.java

public static JSONObject getJSONFromUrl(String url) {

    // Make HTTP request
    try {//from   w w  w. j a  v  a2  s.c  om
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    }

    catch (UnsupportedEncodingException e) {
        Log.e("UnsupportedEncodingException", e.toString());
    }

    catch (ClientProtocolException e) {
        Log.e("ClientProtocolException", e.toString());
    }

    catch (IOException e) {
        Log.e("IOException", e.toString());
    }

    try {
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader reader = new BufferedReader(isr, 8);
        StringBuilder sb = new StringBuilder();
        String line;

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

    catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    }

    catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}