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:windsekirun.qrreader.util.JsonReceiver.java

public String makeJsonCall(String url, int method, List<NameValuePair> params) {
    try {/* www  .  j a  va  2s.co m*/
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity;
        HttpResponse httpResponse = null;

        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            if (params != null)
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            httpResponse = httpClient.execute(httpPost);
        } else if (method == GET) {
            if (params != null) {
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);
            httpResponse = httpClient.execute(httpGet);
        }
        assert httpResponse != null;
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

}

From source file:com.afrisoftech.lib.PDF2ExcelConverter.java

public static void convertPDf2Excel(String pdfFile2Convert) throws Exception {
    if (pdfFile2Convert.length() < 3) {
        System.out.println("File to convert is mandatory!");
        javax.swing.JOptionPane.showMessageDialog(null, "File to convert is mandatory!");
    } else {//  w w w .j  a v a 2  s  . c  om

        final String apiKey = "ktxpfvf0i5se";
        final String format = "xlsx-single".toLowerCase();
        final String pdfFilename = pdfFile2Convert;

        if (!formats.contains(format)) {
            System.out.println("Invalid output format: \"" + format + "\"");
        }

        // Avoid cookie warning with default cookie configuration
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();

        File inputFile = new File(pdfFilename);

        if (!inputFile.canRead()) {
            System.out.println("Can't read input PDF file: \"" + pdfFilename + "\"");
            javax.swing.JOptionPane.showMessageDialog(null, "File to convert is mandatory!");
        }

        try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .build()) {
            HttpPost httppost = new HttpPost("https://pdftables.com/api?format=" + format + "&key=" + apiKey);
            FileBody fileBody = new FileBody(inputFile);

            HttpEntity requestBody = MultipartEntityBuilder.create().addPart("f", fileBody).build();
            httppost.setEntity(requestBody);

            System.out.println("Sending request");

            try (CloseableHttpResponse response = httpclient.execute(httppost)) {
                if (response.getStatusLine().getStatusCode() != 200) {
                    System.out.println(response.getStatusLine());
                    javax.swing.JOptionPane.showMessageDialog(null,
                            "Internet connection is a must. Consult IT administrator for further assistance");
                }
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    final String outputFilename = getOutputFilename(pdfFilename,
                            format.replaceFirst("-.*$", ""));
                    System.out.println("Writing output to " + outputFilename);

                    final File outputFile = new File(outputFilename);
                    FileUtils.copyInputStreamToFile(resEntity.getContent(), outputFile);
                    if (java.awt.Desktop.isDesktopSupported()) {
                        try {
                            java.awt.Desktop.getDesktop().open(outputFile);
                        } catch (IOException ex) {
                            javax.swing.JOptionPane.showMessageDialog(new java.awt.Frame(), ex.getMessage());
                            ex.printStackTrace(); //Exceptions.printStackTrace(ex);
                        }
                    }
                } else {
                    System.out.println("Error: file missing from response");
                    javax.swing.JOptionPane.showMessageDialog(null,
                            "Error: file missing from response! Internet connection is a must.");
                }
            }
        }
    }
}

From source file:fossasia.valentina.bodyapp.sync.Sync.java

/**
 * Method which makes all the POST calls
 * //from w  w  w .  java2 s .  c  om
 * @param url
 * @param json
 * @return
 */
public String POST(String url, String json, int conTimeOut, int socTimeOut) {

    InputStream inputStream = null;
    String result = "";
    try {

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, conTimeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, socTimeOut);
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost(url);
        StringEntity se = new StringEntity(json);
        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        HttpResponse httpResponse = httpclient.execute(httpPost);
        inputStream = httpResponse.getEntity().getContent();

        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";
        System.out.println(result + "result");

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

From source file:com.mycompany.horus.ServiceListener.java

private String getWsdlServicos() throws IOException {
    String resp;/*ww  w.ja  v a  2 s  .  com*/
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(SERVICE_URL);
    HttpResponse response = httpclient.execute(post);
    HttpEntity respEntity = response.getEntity();
    resp = EntityUtils.toString(respEntity);
    return resp;
}

From source file:org.wso2.carbon.tfl.realtime.TflStream.java

public static void send(ArrayList<String> jsonList, String endPoint) {
    for (String data : jsonList) {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(endPoint);
        try {/*from w ww. j av  a 2  s  . c  o m*/
            StringEntity entity = new StringEntity(data);
            post.setEntity(entity);
            HttpResponse response = client.execute(post);
            log.info("data sent : " + data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.cognifide.qa.bb.aem.content.RequestBuilder.java

/**
 * Creates HttpPost request for upload/*from  www .  ja  v a2 s  .  com*/
 *
 * @return HttpPost instance
 */
public HttpPost createUploadRequest() {
    return new HttpPost(String.format(REQUEST_STRING, authorIp, Commands.UPLOAD.getCommand()));
}

From source file:ar.edu.ubp.das.src.chat.actions.LogoutAction.java

@Override
public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, RuntimeException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        String authToken = String.valueOf(request.getSession().getAttribute("token"));

        HttpPost httpPost = new HttpPost("http://25.136.78.82:8080/logout/");
        httpPost.addHeader("Authorization", "BEARER " + authToken);
        httpPost.addHeader("accept", "application/json");

        CloseableHttpResponse postResponse = httpClient.execute(httpPost);

        HttpEntity responseEntity = postResponse.getEntity();
        StatusLine responseStatus = postResponse.getStatusLine();
        String restResp = EntityUtils.toString(responseEntity);

        if (responseStatus.getStatusCode() != 200) {
            throw new RuntimeException(restResp);
        }//from w w w. j  a  v a2 s . co m

        request.getSession().removeAttribute("token");

        return mapping.getForwardByName("success");

    } catch (IOException | RuntimeException e) {
        request.setAttribute("message", "Error al realizar logout: " + e.getMessage());
        response.setStatus(400);
        return mapping.getForwardByName("failure");
    }
}

From source file:org.wikipedia.vlsergey.secretary.jwpf.actions.Review.java

public Review(boolean bot, Revision revision, String token, String comment, Boolean unapprove,
        Integer flag_accuracy) {//from ww w. jav a  2 s .c  om
    super(bot);

    log.info("[action=review]: " + revision + "; " + token + "; " + comment + "; " + unapprove + "; "
            + flag_accuracy);

    HttpPost postMethod = new HttpPost("/api.php");
    MultipartEntity multipartEntity = new MultipartEntity();
    setMaxLag(multipartEntity);
    setFormatXml(multipartEntity);

    setParameter(multipartEntity, "action", "review");
    setParameter(multipartEntity, "token", token);

    setParameter(multipartEntity, "revid", revision.getId().toString());

    if (comment != null)
        setParameter(multipartEntity, "comment", comment);
    if (unapprove != null)
        setParameter(multipartEntity, "unapprove", "" + unapprove);
    if (flag_accuracy != null)
        setParameter(multipartEntity, "flag_accuracy", "" + flag_accuracy);

    postMethod.setEntity(multipartEntity);
    msgs.add(postMethod);
}

From source file:service.xml.YmlLoaderServiceTest.java

@Test
public void test() {
    HttpClient client = new DefaultHttpClient();
    try {//from ww w  .  j a va 2  s  .  co m
        Configuration config = new Configuration(IConstants.TEST_PROPERTIES);

        HttpPost post = new HttpPost(config.getConfigProperties().getProperty("test.url"));
        String testXmlPath = config.getConfigProperties().getProperty("test.xml.path");
        assertNotNull(testXmlPath);

        logger.info("Working with xml: " + testXmlPath);

        InputStreamEntity entity = new InputStreamEntity(new FileInputStream(testXmlPath), -1);
        entity.setChunked(true);

        entity.setContentType("application/xml");
        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            logger.info(line);
            assertEquals("Export complete", line);
        }
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:com.restfiddle.handler.http.PostHandler.java

public RfResponseDTO process(RfRequestDTO rfRequestDTO) throws IOException {
    RfResponseDTO response = null;/*from www. ja v  a  2  s  .  c  om*/
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(rfRequestDTO.getApiUrl());
    httpPost.addHeader("Content-Type", "application/json");
    httpPost.setEntity(new StringEntity(rfRequestDTO.getApiBody()));
    try {
        response = processHttpRequest(httpPost, httpclient);
    } finally {
        httpclient.close();
    }
    return response;
}