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.streaming.sweetplayer.utils.JSONParser.java

public JSONObject getJSONFromUrl(String url) {
    // Making HTTP request
    try {//from   w  ww  . java  2 s.  c  o m
        // DefaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\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;
}

From source file:price.calculation.gateway.service.impl.PriceRequesterServiceImpl.java

@Override
public Response calulatePrice(Product product) {
    res = null;//from  w  w  w .  j  a v a 2  s  .c  o m
    final HttpClient client = new DefaultHttpClient();
    final HttpPost post = new HttpPost(url);
    try {
        post.setHeader("Content-type", "application/json");
        post.setEntity(new StringEntity(product.toJson()));
    } catch (UnsupportedEncodingException ex) {
        res = new Response("Invalid encoding", 400);
        Logger.getLogger(PriceRequesterServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    TimerTask calculationTask = new TimerTask() {

        @Override
        public void run() {
            if (post != null) {
                res = new CalculationTimedOut();
                post.abort();
            }
        }

    };
    new Timer(true).schedule(calculationTask, timeout * 1000);
    try {
        HttpResponse httpResponse = client.execute(post);
        String jsonResp = EntityUtils.toString(httpResponse.getEntity());
        res = CalculatedPrice.fromJson(jsonResp);
    } catch (IOException ex) {
        res = new Response("Service Unavailable", 503);
        Logger.getLogger(PriceRequesterServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    return res;
}

From source file:com.shmsoft.dmass.lotus.SolrConnector.java

protected void sendPostCommand(String point, String param) throws Exception {
    HttpClient httpClient = new DefaultHttpClient();

    HttpPost request = new HttpPost(point);
    StringEntity params = new StringEntity(param, HTTP.UTF_8);
    params.setContentType("text/xml");

    request.setEntity(params);//from w  ww. j  a  va2  s .  c o  m

    HttpResponse response = httpClient.execute(request);
    response.getStatusLine().getStatusCode();
}

From source file:org.dojotoolkit.zazl.internal.XMLHttpRequestUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String xhrRequest(String shrDataString) {
    InputStream is = null;/*from  w ww  .j a v  a2  s. c  o m*/
    String json = null;

    try {
        logger.logp(Level.FINER, XMLHttpRequestUtils.class.getName(), "xhrRequest",
                "shrDataString [" + shrDataString + "]");

        Map<String, Object> xhrData = (Map<String, Object>) JSONParser.parse(new StringReader(shrDataString));
        String url = (String) xhrData.get("url");
        String method = (String) xhrData.get("method");
        List headers = (List) xhrData.get("headers");
        URL requestURL = createURL(url);
        URI uri = new URI(requestURL.toString());

        HashMap httpMethods = new HashMap(7);
        httpMethods.put("DELETE", new HttpDelete(uri));
        httpMethods.put("GET", new HttpGet(uri));
        httpMethods.put("HEAD", new HttpHead(uri));
        httpMethods.put("OPTIONS", new HttpOptions(uri));
        httpMethods.put("POST", new HttpPost(uri));
        httpMethods.put("PUT", new HttpPut(uri));
        httpMethods.put("TRACE", new HttpTrace(uri));
        HttpUriRequest request = (HttpUriRequest) httpMethods.get(method.toUpperCase());

        if (request.equals(null)) {
            throw new Error("SYNTAX_ERR");
        }

        for (Object header : headers) {
            StringTokenizer st = new StringTokenizer((String) header, ":");
            String name = st.nextToken();
            String value = st.nextToken();
            request.addHeader(name, value);
        }

        HttpClient client = new DefaultHttpClient();

        HttpResponse response = client.execute(request);
        Map headerMap = new HashMap();

        HeaderIterator headerIter = response.headerIterator();

        while (headerIter.hasNext()) {
            Header header = headerIter.nextHeader();
            headerMap.put(header.getName(), header.getValue());
        }

        int status = response.getStatusLine().getStatusCode();
        String statusText = response.getStatusLine().toString();

        is = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        StringBuffer sb = new StringBuffer();

        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append('\n');
        }
        Map m = new HashMap();
        m.put("status", new Integer(status));
        m.put("statusText", statusText);
        m.put("responseText", sb.toString());
        m.put("headers", headerMap.toString());
        StringWriter w = new StringWriter();
        JSONSerializer.serialize(w, m);
        json = w.toString();
        logger.logp(Level.FINER, XMLHttpRequestUtils.class.getName(), "xhrRequest", "json [" + json + "]");
    } catch (Throwable e) {
        logger.logp(Level.SEVERE, XMLHttpRequestUtils.class.getName(), "xhrRequest",
                "Failed request for [" + shrDataString + "]", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return json;
}

From source file:org.dataconservancy.ui.it.support.ApproveRegistrationRequest.java

public HttpPost asHttpPost() {
    HttpPost form = new HttpPost(config.getAdminRegistrationUrl().toString());
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("userIdsToApprove", emailAddress));
    params.add(new BasicNameValuePair(STRIPES_EVENT, "submit"));

    UrlEncodedFormEntity entity = null;//from  ww  w .  j a v  a 2  s  .  c om
    try {
        entity = new UrlEncodedFormEntity(params, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    form.setEntity(entity);
    return form;
}