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.odo.kcl.mobileminer.OpenBmapCellRequest.java

@Override
protected Object doInBackground(Object... cellSpec) {
    if (cellSpec.length < 4)
        return null;
    String Mcc, Mnc, Lac, Id;/* w w w  . j a v  a  2 s . c o m*/
    Mcc = (String) cellSpec[0];
    Mnc = (String) cellSpec[1];
    Lac = (String) cellSpec[2];
    Id = (String) cellSpec[3];
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.openbmap.org/api/getGPSfromGSM.php");
    List<NameValuePair> postData = new ArrayList<NameValuePair>(4);
    postData.add(new BasicNameValuePair("mcc", Mcc));
    postData.add(new BasicNameValuePair("mnc", Mnc));
    postData.add(new BasicNameValuePair("lac", Lac));
    postData.add(new BasicNameValuePair("cell_id", Id));
    try {
        String XMLdump, Lat, Long, poly, polyDump;
        Lat = null;
        Long = null;
        poly = null;
        post.setEntity(new UrlEncodedFormEntity(postData));
        XMLdump = EntityUtils.toString(client.execute(post).getEntity());
        //Log.i("MobileMiner",XMLdump);
        Matcher latMatch = latPattern.matcher(XMLdump);
        while (latMatch.find())
            Lat = latMatch.group(1);
        Matcher longMatch = longPattern.matcher(XMLdump);
        while (longMatch.find())
            Long = longMatch.group(1);
        Matcher polyMatch = polyPattern.matcher(XMLdump);
        while (polyMatch.find())
            poly = polyMatch.group(1);
        //Log.i("MobileMiner","Lat "+Lat);
        //Log.i("MobileMiner","Long "+Long);
        //Log.i("MobileMiner","Poly "+poly);
        if (poly != null) {
            List<String> points = new ArrayList<String>();
            String[] point;
            String[] polyChunks = poly.split(",");
            for (String chunk : polyChunks) {
                point = chunk.split("\\s+");
                points.add("[" + point[1] + "," + point[0] + "]");
            }
            polyDump = "[" + TextUtils.join(",", points.subList(0, points.size() - 1)) + "]";
            //Log.i("MobileMiner",polyDump);
        } else {
            polyDump = null;
        }
        if (Lat != null && Long != null) {
            return new String[] { Lat, Long, polyDump };
        } else {
            return null;
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    // TODO Auto-generated method stub
    return null;
}

From source file:com.predic8.membrane.servlet.test.ReleaseConfigurationTest.java

@Test
public void testReachable() throws ClientProtocolException, IOException {
    String secret = "Web Services";
    HttpClient hc = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(getBaseURL());
    post.setEntity(new StringEntity(secret));
    HttpResponse res = hc.execute(post);
    assertEquals(200, res.getStatusLine().getStatusCode());

    AssertUtils.assertContains(secret, EntityUtils.toString(res.getEntity()));
}

From source file:bigbluej.Crawler.java

public String post(String url) throws IOException {
    System.out.println("post> " + url);
    HttpClient client = httpClientFactory.create();
    HttpPost request = new HttpPost(url);
    HttpResponse response = client.execute(request);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuilder s = new StringBuilder();
    String line;//  w  w  w  .  j a  v  a2s.  c  o  m
    while ((line = rd.readLine()) != null) {
        s.append(line);
    }
    return s.toString();
}

From source file:it.av.fac.driver.APIClient.java

public String queryRequest(String userToken, String resource) {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        HttpPost httpPost = new HttpPost(endpoint + QUERY_PATH);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("request", URLEncoder.encode(resource, "UTF-8")));
        nvps.add(new BasicNameValuePair("token", URLEncoder.encode(userToken, "UTF-8")));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));

        // Create a custom response handler
        ResponseHandler<String> responseHandler = (final HttpResponse response) -> {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }/*w  w w  . j a  va  2 s . co m*/
        };

        return httpclient.execute(httpPost, responseHandler);
    } catch (IOException ex) {
        Logger.getLogger(APIClient.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:com.networknt.light.server.handler.loader.Loader.java

public static void login(String host, String userId, String password) throws Exception {
    Map<String, Object> inputMap = new HashMap<String, Object>();
    inputMap.put("category", "user");
    inputMap.put("name", "signInUser");
    inputMap.put("readOnly", false);
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("userIdEmail", userId);
    data.put("password", password);
    data.put("rememberMe", true);
    data.put("clientId", "example@Browser");
    inputMap.put("data", data);

    HttpPost httpPost = new HttpPost(host + "/api/rs");
    StringEntity input = new StringEntity(
            ServiceLocator.getInstance().getMapper().writeValueAsString(inputMap));
    input.setContentType("application/json");
    httpPost.setEntity(input);//from w  w w .  j a  v  a  2 s.  co  m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {

        //System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        //System.out.println("json = " + json);
        Map<String, Object> jsonMap = ServiceLocator.getInstance().getMapper().readValue(json,
                new TypeReference<HashMap<String, Object>>() {
                });
        jwt = (String) jsonMap.get("accessToken");
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        response.close();
    }
}

From source file:com.sonatype.nexus.perftest.ossrh.ProjectProvisioningOperation.java

@Override
public void perform(ClientRequestInfo requestInfo) throws Exception {
    DefaultHttpClient httpclient = getHttpClient();

    StringBuilder url = new StringBuilder(nexusBaseurl);
    if (!nexusBaseurl.endsWith("/")) {
        url.append("/");
    }//ww w .j a v a  2 s  . c o  m
    url.append("service/siesta/onboard");

    url.append("?users=").append("jvanzyl");
    url.append("&groupId=").append(String.format("test.nexustaging-%03d", requestInfo.getRequestId()));

    HttpPost request = new HttpPost(url.toString());

    HttpResponse response = httpclient.execute(request);

    String json = EntityUtils.toString(response.getEntity());

    if (!isSuccess(response)) {
        throw new IOException(request.toString() + " : " + response.getStatusLine().toString());
    }
}

From source file:io.selendroid.server.util.HttpClientUtil.java

public static HttpResponse executeRequest(String url, HttpMethod method) throws Exception {
    HttpRequestBase request = null;// w  w  w .j  a  v  a 2 s  . c  om
    if (HttpMethod.GET.equals(method)) {
        request = new HttpGet(url);
    } else if (HttpMethod.POST.equals(method)) {
        request = new HttpPost(url);
    } else if (HttpMethod.DELETE.equals(method)) {
        request = new HttpDelete(url);
    } else {
        throw new RuntimeException("Provided HttpMethod not supported");
    }
    return getHttpClient().execute(request);
}

From source file:com.vmware.identity.rest.core.client.RequestFactory.java

public static HttpPost createPostRequest(URI uri, AccessToken token, Object entity)
        throws ClientException, JsonProcessingException {
    return prepareRequest(new HttpPost(uri), token, entity);
}

From source file:service.json.StatisticHandlerSeviceTest.java

@Test
public void test() {
    HttpClient client = new DefaultHttpClient();

    try {//  w  w w .ja  va2s.  c o m
        Configuration config = new Configuration(IConstants.TEST_PROPERTIES);

        HttpPost post = new HttpPost(config.getConfigProperties().getProperty("test.statistic.url"));

        File file = new File(".\\config\\test.json");
        assertTrue(file.exists() && file.isFile());

        FileEntity entity = new FileEntity(file, "application/json");
        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        assertNotNull(rd);

        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:com.rastating.droidbeard.net.ErrorReportTask.java

public JSONObject postData(String url, JSONObject obj) {
    HttpClient client = new DefaultHttpClient();
    String json = obj.toString();

    try {/*from  ww w.ja va  2 s.c  o  m*/
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-type", "application/json");

        StringEntity se = new StringEntity(obj.toString());
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);

        HttpResponse response = client.execute(post);
        String retval = EntityUtils.toString(response.getEntity());

        return new JSONObject(retval);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}