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.jts.main.helper.Http.java

public static String sendPost(String url, URLParameter param) {
    String retval = "";
    HttpClient httpClient = new DefaultHttpClient();
    try {/*from ww w  . jav a2  s.  c  o m*/
        HttpPost request = new HttpPost(My.base_url + url);
        StringEntity params = new StringEntity(param.get());
        request.addHeader("content-type", "application/x-www-form-urlencoded");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        // handle response here...
        retval = org.apache.http.util.EntityUtils.toString(response.getEntity());
        org.apache.http.util.EntityUtils.consume(response.getEntity());
    } catch (IOException | ParseException ex) {
        errMsg = ex.getMessage();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return retval;
}

From source file:net.nordist.lloydproof.HttpJSONClient.java

public HttpJSONClient(String url) {
    super();
    httpClient = new DefaultHttpClient();
    httpRequest = new HttpPost(url);
}

From source file:core.VirusTotalAPIHelper.java

public static CloseableHttpResponse reScan(String sha256) {
    try {//ww  w  .ja v  a 2 s . c  o  m
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpsScanFilePost = new HttpPost(httpsPost + "file/rescan");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("apikey", apiKey);
        builder.addTextBody("resource", sha256);
        HttpEntity multipart = builder.build();
        httpsScanFilePost.setEntity(multipart);

        CloseableHttpResponse response = client.execute(httpsScanFilePost);
        return response;
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    return null;
}

From source file:org.artags.android.app.util.http.HttpUtil.java

/**
 * Post data and attachements/*from   ww  w. j  a v a  2  s .c o  m*/
 * @param url The POST url
 * @param params Parameters
 * @param files Files
 * @return The return value
 * @throws HttpException If an error occurs
 */
public static String post(String url, HashMap<String, String> params, HashMap<String, File> files)
        throws HttpException {
    String ret = "";
    try {
        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost(url);

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for (String key : files.keySet()) {
            FileBody bin = new FileBody(files.get(key));
            reqEntity.addPart(key, bin);
        }

        for (String key : params.keySet()) {
            String val = params.get(key);

            reqEntity.addPart(key, new StringBody(val, Charset.forName("UTF-8")));
        }
        post.setEntity(reqEntity);
        HttpResponse response = client.execute(post);

        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            ret = EntityUtils.toString(resEntity);
            Log.i("ARTags:HttpUtil:Post:Response", ret);
        }

        //return response;
    } catch (Exception e) {
        Log.e("ARTags:HttpUtil", "Error : " + e.getMessage());
        throw new HttpException(e.getMessage());
    }
    return ret;
}

From source file:io.fabric8.maven.docker.access.util.RequestUtil.java

public static HttpUriRequest newPost(String url, String body) {
    HttpPost post = new HttpPost(url);
    if (body != null) {
        post.setEntity(new StringEntity(body, Charset.defaultCharset()));
    }//from   w w  w.j  a va 2  s .c o  m
    return addDefaultHeaders(post);
}

From source file:util.Slack.java

private static void sendMsg(StringEntity jsonMsg, String url) throws IOException {
    HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead 

    try {//from w w  w  .  ja va2  s  . c  om
        HttpPost request = new HttpPost(url);
        request.addHeader("content-type", "application/json; charset=UTF-8");
        request.setEntity(jsonMsg);
        HttpResponse response = httpClient.execute(request);
        System.out.println(response);
    } finally {
        httpClient.getConnectionManager().shutdown(); //Deprecated
    }
}

From source file:com.normalexception.app.rx8club.httpclient.ClientUtils.java

/**
 * Report an HttpPost object/*from  ww  w  .  j a v a  2s.  c om*/
 * @param address   The address to request
 * @return         An httppost with proper headers
 */
public static HttpPost getHttpPost(String address) {
    HttpPost hp = (HttpPost) getCommonHeaders(new HttpPost(address));
    return hp;
}

From source file:org.megam.api.http.TransportMachinery.java

public static TransportResponse post(TransportTools nuts) throws ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(nuts.urlString());
    System.out.println("NUTS" + nuts.toString());
    if (nuts.headers() != null) {
        for (Map.Entry<String, String> headerEntry : nuts.headers().entrySet()) {
            httppost.addHeader(headerEntry.getKey(), headerEntry.getValue());
        }//from w  w  w  .j a v a 2 s.com
    }
    if (nuts.fileEntity() != null) {
        httppost.setEntity(nuts.fileEntity());
    }
    if (nuts.pairs() != null && (nuts.contentType() == null)) {
        httppost.setEntity(new UrlEncodedFormEntity(nuts.pairs()));
    }

    if (nuts.contentType() != null) {
        httppost.setEntity(new StringEntity(nuts.contentString(), nuts.contentType()));
    }
    TransportResponse transportResp = null;
    System.out.println(httppost.toString());
    try {
        HttpResponse httpResp = httpclient.execute(httppost);
        transportResp = new TransportResponse(httpResp.getStatusLine(), httpResp.getEntity(),
                httpResp.getLocale());
    } finally {
        httppost.releaseConnection();
    }
    return transportResp;

}

From source file:com.connectsdk.etc.helper.HttpMessage.java

public static HttpPost getHttpPost(String uri) {
    HttpPost post = null;// ww w .j a va 2s.c  o m
    try {
        post = new HttpPost(uri);
        post.setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_TEXT_XML);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

    return post;
}

From source file:mobilebank.Json.java

public JSONObject getJson(String url) throws ParseException {

    HttpPost httppost = new HttpPost(baseUrl + url);
    // Depends on your web service
    httppost.setHeader("Content-type", "application/x-www-form-urlencoded");

    InputStream inputStream = null;
    String result = null;/*www .j  a  va2 s .c o m*/
    try {
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (Exception e) {
        // Oops
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
        } catch (Exception squish) {
        }
    }
    JSONParser parser = new JSONParser();
    JSONObject jObject = (JSONObject) parser.parse(result);
    return jObject;

}