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:org.wso2.api.client.restcalls.StoreAPIInvoker.java

public static String loginToStore(HttpClient httpClient) throws IOException {
    HttpPost storeLoginRequest = new HttpPost(APIDownloaderConstant.LOGIN_API_CALL);
    HttpResponse storeLoginResponse = httpClient.execute(storeLoginRequest);

    for (Header header : storeLoginResponse.getAllHeaders()) {
        if (header.getName().equals("Set-Cookie")) {
            return header.getValue().split(";", 2)[0];
        }/*from w w  w.  j a v a 2 s.c  o m*/
    }
    return null;
}

From source file:costumetrade.common.util.HttpClientUtils.java

public static String doPost(String url, HttpEntity httpEntity) {

    logger.info("?{}....", url);

    try (CloseableHttpClient httpclient = HttpClients.custom().build();) {
        HttpPost request = new HttpPost(url);
        request.setEntity(httpEntity);//  w w w.j  a  v  a2s  . co  m
        try (CloseableHttpResponse response = httpclient.execute(request);) {
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("?{}???{}", url, statusCode);
            if (HttpStatus.SC_OK == statusCode) {
                return IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8.name());
            }
            throw new RuntimeException(String.format("?%s???%s", url, statusCode));
        }
    } catch (Exception e) {
        throw new RuntimeException(String.format("%s", url), e);
    }
}

From source file:io.tourniquet.junit.http.rules.examples.HttpClientHelper.java

public static HttpPost post(String url, BasicNameValuePair... params) throws UnsupportedEncodingException {

    HttpPost post = new HttpPost(url);
    if (params.length > 0) {
        post.setEntity(new UrlEncodedFormEntity(Arrays.asList(params)));
    }/*  w w  w .j ava 2s . co m*/
    return post;
}

From source file:jsonclient.JsonClient.java

private static String postToURL(String url, String message, DefaultHttpClient httpClient)
        throws IOException, IllegalStateException, UnsupportedEncodingException, RuntimeException {
    HttpPost postRequest = new HttpPost(url);

    StringEntity input = new StringEntity(message);
    input.setContentType("application/json");
    postRequest.setEntity(input);//  www .ja  v a 2s  .c  om

    HttpResponse response = httpClient.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

    String output;
    StringBuffer totalOutput = new StringBuffer();
    //System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        //System.out.println(output);
        totalOutput.append(output);
    }
    return totalOutput.toString();
}

From source file:Main.java

public static void sendData(final String targetUrl, final String idField, final String tempField,
        final String feelingField, final String symptomsField, final String id, final String temp,
        final String feeling, final ArrayList<String> symptoms) {
    new Thread(new Runnable() {
        public void run() {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(targetUrl);

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair(idField, id));
                nameValuePairs.add(new BasicNameValuePair(tempField, temp));
                nameValuePairs.add(new BasicNameValuePair(feelingField, feeling));
                for (int i = 0; i < symptoms.size(); i++) {
                    nameValuePairs.add(new BasicNameValuePair(symptomsField, symptoms.get(i)));
                }/*from w w  w .  j  a  v a2 s.  co  m*/
                // Must put this extra pageHistory field here, otherwise Forms will reject the
                // symptoms.
                nameValuePairs.add(new BasicNameValuePair("pageHistory", "0,1"));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:nayan.netty.client.FileUploadClient.java

private static void uploadFile() throws Exception {
    File file = new File("small.jpg");

    HttpEntity httpEntity = MultipartEntityBuilder.create()
            .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName()).build();

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://localhost:8080");

    httppost.setEntity(httpEntity);/*from   w  w  w  .  ja  v  a  2s  .  com*/
    System.out.println("executing request " + httppost.getRequestLine());

    CloseableHttpResponse response = httpclient.execute(httppost);

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
        System.out.println("Response content length: " + resEntity.getContentLength());
    }

    EntityUtils.consume(resEntity);

    response.close();
}

From source file:com.srotya.tau.ui.BapiLoginDAO.java

public static Entry<String, String> authenticate(String authURL, String username, String password)
        throws Exception {
    CloseableHttpClient client = Utils.buildClient(authURL, 3000, 5000);
    HttpPost authRequest = new HttpPost(authURL);
    Gson gson = new Gson();
    JsonObject obj = new JsonObject();
    obj.addProperty(USERNAME, username);
    obj.addProperty(PASSWORD, password);
    StringEntity entity = new StringEntity(gson.toJson(obj), ContentType.APPLICATION_JSON);
    authRequest.setEntity(entity);/*from   ww  w  .  java2  s .  c om*/
    CloseableHttpResponse response = client.execute(authRequest);
    if (response.getStatusLine().getStatusCode() == 200) {
        String tokenPair = EntityUtils.toString(response.getEntity());
        JsonArray ary = gson.fromJson(tokenPair, JsonArray.class);
        obj = ary.get(0).getAsJsonObject();
        String token = obj.get(X_SUBJECT_TOKEN).getAsString();
        String hmac = obj.get(HMAC).getAsString();
        return new AbstractMap.SimpleEntry<String, String>(token, hmac);
    } else {
        System.err.println("Login failed:" + response.getStatusLine().getStatusCode() + "\t"
                + response.getStatusLine().getReasonPhrase());
        return null;
    }
}

From source file:com.starbucks.apps.HttpUtils.java

public static HttpInvocationContext doPost(final String payload, String contentType, String url)
        throws IOException {

    HttpUriRequest request = new HttpPost(url);
    return invoke(request, payload, contentType);
}

From source file:org.opencastproject.remotetest.server.resource.ComposerResources.java

public static HttpResponse encode(TrustedHttpClient client, String mediapackage, String audioSourceTrackId,
        String videoSourceTrackId, String profileId) throws Exception {
    HttpPost post = new HttpPost(getServiceUrl() + "encode");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("mediapackage", mediapackage));
    params.add(new BasicNameValuePair("audioSourceTrackId", audioSourceTrackId));
    params.add(new BasicNameValuePair("videoSourceTrackId", videoSourceTrackId));
    params.add(new BasicNameValuePair("profileId", profileId));
    post.setEntity(new UrlEncodedFormEntity(params));
    return client.execute(post);
}

From source file:com.TaxiDriver.jy.DriverQuery.java

public static String getETA(String url, String fromLat, String fromLongi, String toLat, String toLongi,
        String type) {//from   ww  w  .  j a  v  a2 s  .co  m

    HttpPost postJob = new HttpPost(HttpHelper.domain + "getduration.php");

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 4900;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient clientJob = new DefaultHttpClient(httpParameters);

    try {
        List<NameValuePair> infoJob = new ArrayList<NameValuePair>(2);
        infoJob.add(new BasicNameValuePair("url", url));

        infoJob.add(new BasicNameValuePair("fromLongi", fromLongi));
        infoJob.add(new BasicNameValuePair("fromLat", fromLat));
        infoJob.add(new BasicNameValuePair("toLongi", toLongi));
        infoJob.add(new BasicNameValuePair("toLat", toLat));
        infoJob.add(new BasicNameValuePair("type", type));

        postJob.setEntity(new UrlEncodedFormEntity(infoJob));

        HttpResponse response = clientJob.execute(postJob);

        String jsonString = HttpHelper.request(response);
        String duration = jsonString.trim();

        return duration;

    } catch (Exception ex) {
        return null;
    }

}