Example usage for org.apache.http.client.utils URIBuilder URIBuilder

List of usage examples for org.apache.http.client.utils URIBuilder URIBuilder

Introduction

In this page you can find the example usage for org.apache.http.client.utils URIBuilder URIBuilder.

Prototype

public URIBuilder() 

Source Link

Document

Constructs an empty instance.

Usage

From source file:org.apache.james.ESReporterTest.java

private URIBuilder baseUri() {
    return new URIBuilder().setScheme("http").setHost("localhost")
            .setPort(server.getProbe(JmapGuiceProbe.class).getJmapPort()).setCharset(Charsets.UTF_8);
}

From source file:msgclient.ServerConnection.java

public JSONObject makePostToServer(String path, String json_data) {
    CloseableHttpResponse response;//from w  ww  .  ja va2s.c o  m
    JSONObject jsonObject = null;

    // Send a POST request to the server
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        // Create a URI            
        URI uri = new URIBuilder().setScheme(PROTOCOL_TYPE).setHost(mServerName + ":" + mPort).setPath(path)
                .build();
        HttpPost httppost = new HttpPost(uri);
        //System.out.println(httppost.getURI());
        StringEntity entity = new StringEntity(json_data, ContentType.create("plain/text", "UTF-8"));
        entity.setContentType("application/json");
        httppost.setEntity(entity);

        response = httpClient.execute(httppost);
        if (response.getStatusLine().getStatusCode() == 200) {
            String jsonData = IOUtils.toString(response.getEntity().getContent());
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(jsonData);
            jsonObject = (JSONObject) obj;
        } else {
            System.out.println(
                    "Received status code " + response.getStatusLine().getStatusCode() + " from server");
        }

        response.close();
        httpClient.close();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }

    return jsonObject;
}

From source file:Vdisk.java

public void download_file(String filepath, String local_filepath) throws URISyntaxException, IOException {
    URI uri = new URIBuilder().setScheme("https").setHost("api.weipan.cn/2/files/sandbox/").setPath(filepath)
            .setParameter("access_token", this.access_token).build();
    CloseableHttpClient getClient = HttpClients.createDefault();
    if (access_token == null)
        this.get_access_token();
    HttpGet httpGet = new HttpGet(uri);
    try (CloseableHttpResponse response = getClient.execute(httpGet)) {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == 200) {
            File file = new File(local_filepath);
            try (FileOutputStream fout = new FileOutputStream(file)) {
                String content = EntityUtils.toString(response.getEntity());
                System.out.println(content.length());
                byte contents[] = content.getBytes(Charset.forName("ISO-8859-1"));
                fout.write(contents);//from   w w  w.j  a  va  2s . c o  m
            }
        }
    } finally {
        getClient.close();
    }
}

From source file:org.wso2.security.tools.automation.manager.scanner.dynamicscanner.containerbased.zap.ZapClient.java

/**
 * Save a session/*ww  w .j  a  v a  2  s .  c o m*/
 *
 * @param name      Session name
 * @param overwrite Overwrite value
 * @param post      Boolean value to indicate request method (GET or POST)
 * @return HTTP response after executing the HTTP command
 * @throws IOException        If cannot communicate with server
 * @throws URISyntaxException Checked exception thrown to indicate that a string could not be parsed as a
 *                            URI reference
 */
public HttpResponse saveSession(String name, boolean overwrite, Boolean post)
        throws IOException, URISyntaxException {

    URI uri = (new URIBuilder()).setHost(this.host).setPort(this.port).setScheme(this.scheme)
            .setPath(SAVE_SESSION).addParameter("formMethod", post ? POST : GET).addParameter("name", name)
            .addParameter("overwrite", overwrite ? "true" : "false").build();
    LOGGER.trace("Sending request to save session");
    return httpClient.execute(post ? new HttpPost(uri) : new HttpGet(uri));
}

From source file:org.wuspba.ctams.ws.ITBandMemberController.java

@Test
public void testListPerson() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
            .setParameter("person", TestFixture.INSTANCE.andy.getId()).build();

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        assertEquals(doc.getBandMembers().size(), 1);
        testEquality(doc.getBandMembers().get(0), TestFixture.INSTANCE.andyMember);

        EntityUtils.consume(entity);/*from   ww w. j  a  v  a 2s  . co m*/
    }

    uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
            .setParameter("person", "garbage").build();

    httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        assertEquals(doc.getBandMembers().size(), 0);

        EntityUtils.consume(entity);
    }
}