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.ssy.havefunweb.util.WeixinUtil.java

public static JSONObject doPostStr(String url, String outStr) throws UnsupportedEncodingException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new StringEntity(outStr, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);
    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
    JSONObject jsonObject = JSONObject.fromObject(result);
    return jsonObject;
}

From source file:com.gozap.chouti.service.HttpService.java

/**
 *
 * @param uri uri//from   w  w  w .  j a  va2s .c o  m
 * @return
 * @author saint
 * @date 2013-4-17
 */
public static Boolean post(String uri, UrlEncodedFormEntity entity) {
    HttpClient httpClient = HttpClientFactory.getInstance();
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(entity);
    try {
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        String jsonStr = EntityUtils.toString(httpEntity);
        JSONObject json = JSON.parseObject(jsonStr);
        if (!json.getString("code").equals("99999")) {
            LOGGER.error("post uri:" + uri + ":" + entity + "error!" + "error msg: " + jsonStr);
        } else {
            return true;
        }
    } catch (ClientProtocolException e) {
        LOGGER.error("get" + uri + "error", e);
    } catch (IOException e) {
        LOGGER.error("get" + uri + "error", e);
    } finally {
        httpPost.releaseConnection();
    }

    return false;
}

From source file:org.fcrepo.integration.api.FedoraFieldSearchIT.java

@Test
public void testSearchSubmit() throws Exception {
    final HttpPost method = new HttpPost(serverAddress + "search");
    final List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();

    list.add(new BasicNameValuePair("terms", ""));
    list.add(new BasicNameValuePair("offset", "0"));
    list.add(new BasicNameValuePair("maxResults", "1"));
    final UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
    method.setEntity(formEntity);/*from   w  ww .  j a  v a 2 s  . c om*/
    assertEquals(200, getStatus(method));

}

From source file:org.fdroid.enigtext.mms.MmsSendHelper.java

private static byte[] makePost(Context context, MmsConnectionParameters parameters, byte[] mms)
        throws ClientProtocolException, IOException {
    AndroidHttpClient client = null;/*from   w  ww. j av a  2 s  . c  om*/

    try {
        Log.w("MmsSender", "Sending MMS1 of length: " + (mms != null ? mms.length : "null"));
        client = constructHttpClient(context, parameters);
        URI targetUrl = new URI(parameters.getMmsc());

        if (Util.isEmpty(targetUrl.getHost()))
            throw new IOException("Invalid target host: " + targetUrl.getHost() + " , " + targetUrl);

        HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpPost request = new HttpPost(parameters.getMmsc());
        ByteArrayEntity entity = new ByteArrayEntity(mms);

        entity.setContentType("application/vnd.wap.mms-message");

        request.setEntity(entity);
        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
        request.addHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsSendHelper", use);
        throw new IOException("Couldn't parse URI.");
    } finally {
        if (client != null)
            client.close();
    }
}

From source file:org.lazydevs.api.gog.util.HttpClientUtil.java

public static HttpResponse post(HttpClient client, HttpContext context, String url,
        List<NameValuePair> params) {

    HttpPost post = new HttpPost(url);
    HttpResponse response;/*from  w  w w .  ja v a  2  s. c  o  m*/

    post.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));

    try {
        response = client.execute(post, context);
    } catch (IOException e) {
        throw new GogApiException("Error executing POST request. url=" + url, e);
    }

    return response;
}

From source file:service.StatisticsServerTest.java

public void testApp() throws UnsupportedEncodingException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://194.28.132.219:8080/statsserver_archive/rest/");
    StringEntity entity = new StringEntity("{\"age\":\"38\",\"sex\":\"Male\",\"country\":\"Russia\"}");
    entity.setContentType("application/json");
    post.setEntity(entity);//from www  .  j a v  a2  s. c o  m
    try {
        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);
        }
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:br.com.atmatech.sac.webService.WebServiceCadastro.java

public PessoaBeans postWebService(String cnpj) throws IOException {
    PessoaBeans pb = new PessoaBeans();
    HttpPost post = new HttpPost("http://appasp.sefaz.go.gov.br/Sintegra/Consulta/consultar.asp");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("rTipoDoc", "2"));
    nameValuePairs.add(new BasicNameValuePair("tDoc", cnpj));
    nameValuePairs.add(new BasicNameValuePair("tCCE", ""));
    nameValuePairs.add(new BasicNameValuePair("tCNPJ", cnpj));
    nameValuePairs.add(new BasicNameValuePair("tCPF", ""));
    nameValuePairs.add(new BasicNameValuePair("btCGC", "Consultar"));
    nameValuePairs.add(new BasicNameValuePair("zion.SystemAction", "consultarSintegra()"));
    nameValuePairs.add(new BasicNameValuePair("zion.OnSubmited", ""));
    nameValuePairs.add(new BasicNameValuePair("zion.FormElementPosted", "zionFormID_1"));
    nameValuePairs.add(new BasicNameValuePair("zionPostMethod", ""));
    nameValuePairs.add(new BasicNameValuePair("zionRichValidator", "true"));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8));
    HttpResponse response;/*from w  w w.j  a  v a 2 s.com*/

    response = client.execute(post);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String inputLine;
    // Grava pagina no arquivo
    // BufferedWriter out = new BufferedWriter(new FileWriter("./pagina.txt"));
    //        while ((inputLine = rd.readLine()) != null) {
    //            out.write(inputLine);
    //            out.newLine();
    //        }
    // Grava pagina no arquivo
    FileWriter out = new FileWriter("./pagina.txt");
    PrintWriter gravarArq = new PrintWriter(out);
    while ((inputLine = rd.readLine()) != null) {
        gravarArq.print(inputLine + "\n");
    }
    out.close();

    String insc = searchTableWeb("Estadual - CCE :", new BufferedReader(new FileReader("./pagina.txt")));
    if (insc != null) {
        pb.setIe(insc.replaceAll("[.-]", ""));
    }
    pb.setRazao(searchTableWeb("Nome Empresarial:", new BufferedReader(new FileReader("./pagina.txt"))));
    pb.setEndereco(searchTableWeb("Logradouro:", new BufferedReader(new FileReader("./pagina.txt"))));
    pb.setNumero(searchTableWeb("mero:", new BufferedReader(new FileReader("./pagina.txt"))));
    pb.setBairro(searchTableWeb("Bairro:", new BufferedReader(new FileReader("./pagina.txt"))));
    pb.setDistrito(searchTableWeb("pio:", new BufferedReader(new FileReader("./pagina.txt"))));
    String tel = searchTableWeb("Telefone:", new BufferedReader(new FileReader("./pagina.txt")));
    if (tel != null) {
        pb.setTelefone1(tel.replaceAll("[()]", "").replaceAll(" ", ""));
    }
    return pb;
}

From source file:io.kahu.hawaii.util.call.http.PostRequest.java

public PostRequest(RequestDispatcher requestDispatcher, RequestContext<T> context, URI uri, String payload,
        ResponseHandler<HttpResponse, T> responseHandler, CallLogger<T> logger) {
    super(requestDispatcher, context, responseHandler, new HttpPost(uri), logger);
}

From source file:com.eyekabob.util.JSONTask.java

public JSONObject doRequest(String uri) {
    HttpClient client = new DefaultHttpClient();
    HttpRequestBase request = null;//  ww  w.j ava  2s  . co  m
    if ("POST".equals(requestType)) {
        request = new HttpPost(uri);
    } else {
        request = new HttpGet(uri);
    }
    StringBuffer sb = null;

    try {
        HttpResponse response = client.execute(request);
        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        sb = new StringBuffer();
        String line = null;
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // should this be initialized to an empty json object?
    JSONObject result = null;

    try {
        result = new JSONObject(sb.toString());
    } catch (JSONException e) {
        Log.e(getClass().getName(), "Error parsing json response", e);
    }

    return result;
}

From source file:com.swetha.easypark.EasyParkHttpClient.java

/** 
 * @param url of the website and the post request
 * @return http response in string */
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {

    BufferedReader in = null;/*from w w w .  j av  a2  s .c  o  m*/

    try {

        HttpClient client = getHttpClient();

        HttpPost request = new HttpPost(url);

        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(

                postParameters);

        request.setEntity(formEntity);
        Log.i("CustomHttpClient", "before sending request" + request);
        HttpResponse response = client.execute(request);
        Log.i("CustomHttpClient", "after sending request" + response.toString());
        //Log.i("CustomHttpClient", "after sending request response in to string" +response.getEntity().getContent().toString());

        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();

        String result = sb.toString();

        return result;

    }

    finally {

        if (in != null) {

            try {

                in.close();

            } catch (IOException e) {

                Log.e("log_tag", "Error converting result " + e.toString());

                e.printStackTrace();

            }

        }
    }

}