Example usage for org.apache.http.client.entity UrlEncodedFormEntity getContentLength

List of usage examples for org.apache.http.client.entity UrlEncodedFormEntity getContentLength

Introduction

In this page you can find the example usage for org.apache.http.client.entity UrlEncodedFormEntity getContentLength.

Prototype

public long getContentLength() 

Source Link

Usage

From source file:com.thoughtworks.go.util.HttpServiceTest.java

@Test
public void shouldSetTheAcceptHeaderWhilePostingProperties() throws Exception {
    HttpPost post = mock(HttpPost.class);
    String url = "http://url";
    when(httpClientFactory.createPost(url)).thenReturn(post);
    when(post.getURI()).thenReturn(new URI(url));
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(post)).thenReturn(response);

    ArgumentCaptor<UrlEncodedFormEntity> entityCaptor = ArgumentCaptor.forClass(UrlEncodedFormEntity.class);

    service.postProperty(url, "value");

    verify(post).setHeader("Confirm", "true");
    verify(post).setEntity(entityCaptor.capture());

    UrlEncodedFormEntity expected = new UrlEncodedFormEntity(
            Arrays.asList(new BasicNameValuePair("value", "value")));

    UrlEncodedFormEntity actual = entityCaptor.getValue();

    assertEquals(IOUtils.toString(expected.getContent()), IOUtils.toString(actual.getContent()));
    assertEquals(expected.getContentLength(), expected.getContentLength());
    assertEquals(expected.getContentType(), expected.getContentType());
    assertEquals(expected.getContentEncoding(), expected.getContentEncoding());
    assertEquals(expected.isChunked(), expected.isChunked());
}

From source file:in.rab.ordboken.NeClient.java

private void loginMainSite() throws IOException, LoginException {
    ArrayList<BasicNameValuePair> data = new ArrayList<BasicNameValuePair>();

    data.add(new BasicNameValuePair("_save_loginForm", "true"));
    data.add(new BasicNameValuePair("redir", "/success"));
    data.add(new BasicNameValuePair("redirFail", "/fail"));
    data.add(new BasicNameValuePair("userName", mUsername));
    data.add(new BasicNameValuePair("passWord", mPassword));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data);

    URL url = new URL("https://www.ne.se/user/login.jsp");
    HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
    https.setInstanceFollowRedirects(false);
    https.setFixedLengthStreamingMode((int) entity.getContentLength());
    https.setDoOutput(true);/*from ww w  . j a v  a2 s  . c o  m*/

    try {
        OutputStream output = https.getOutputStream();
        entity.writeTo(output);
        output.close();

        Integer response = https.getResponseCode();
        if (response != 302) {
            throw new LoginException("Unexpected response: " + response);
        }

        String location = https.getHeaderField("Location");
        if (!location.contains("/success")) {
            throw new LoginException("Failed to login");
        }
    } finally {
        https.disconnect();
    }
}