Search in sources :

Example 96 with HttpPost

use of org.apache.http.client.methods.HttpPost in project gocd by gocd.

the class HttpService method upload.

public int upload(String url, long size, File artifactFile, Properties artifactChecksums) throws IOException {
    String absolutePath = artifactFile.getAbsolutePath();
    if (!artifactFile.exists()) {
        String message = "Failed to find file [" + absolutePath + "]";
        LOGGER.error(message);
        throw new FileNotFoundException(message);
    }
    LOGGER.info(String.format("Uploading file [%s] to url [%s]", absolutePath, url));
    HttpPost filePost = createHttpPostForUpload(url, size, artifactFile, artifactChecksums);
    try (CloseableHttpResponse response = execute(filePost)) {
        return response.getStatusLine().getStatusCode();
    } catch (IOException e) {
        LOGGER.error("Error while uploading file [" + artifactFile.getAbsolutePath() + "]", e);
        throw e;
    } finally {
        filePost.releaseConnection();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 97 with HttpPost

use of org.apache.http.client.methods.HttpPost in project gocd by gocd.

the class HttpService method createHttpPostForUpload.

private HttpPost createHttpPostForUpload(String url, long size, File artifactFile, Properties artifactChecksums) throws IOException {
    HttpPost filePost = httpClientFactory.createPost(url);
    setSizeHeader(filePost, size);
    filePost.setHeader("Confirm", "true");
    filePost.setEntity(httpClientFactory.createMultipartRequestEntity(artifactFile, artifactChecksums));
    return filePost;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost)

Example 98 with HttpPost

use of org.apache.http.client.methods.HttpPost in project gocd by gocd.

the class HttpServiceTest method shouldSetTheAcceptHeaderWhilePostingProperties.

@Test
public void shouldSetTheAcceptHeaderWhilePostingProperties() throws Exception {
    HttpPost post = mock(HttpPost.class);
    when(httpClientFactory.createPost("url")).thenReturn(post);
    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());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 99 with HttpPost

use of org.apache.http.client.methods.HttpPost in project iosched by google.

the class HttpClientStackTest method testCreatePostRequest.

public void testCreatePostRequest() throws Exception {
    TestRequest.Post request = new TestRequest.Post();
    assertEquals(request.getMethod(), Method.POST);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpPost);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpPost(org.apache.http.client.methods.HttpPost) TestRequest(com.android.volley.mock.TestRequest)

Example 100 with HttpPost

use of org.apache.http.client.methods.HttpPost in project 12306-hunter by xautlx.

the class HttpClientService method postHttpRequest.

/**
	 * POST请求
	 * @param httpclient
	 * @param url
	 * @param parameters
	 * @param cookieData
	 * @return
	 */
private HttpResponse postHttpRequest(HttpClient httpclient, String url, List<NameValuePair> parameters, Map<String, String> cookieData) {
    try {
        logger.debug("------------------------------------------------------------------------");
        logger.debug("POST URL: " + url);
        HttpPost post = new HttpPost(url);
        post.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)");
        if (cookieData != null) {
            boolean first = true;
            StringBuilder cookie = new StringBuilder();
            for (Map.Entry<String, String> me : cookieData.entrySet()) {
                if (first) {
                    first = false;
                } else {
                    cookie.append(";");
                }
                cookie.append(me.getKey() + "=" + me.getValue());
            }
            post.setHeader("Cookie", cookie.toString());
        }
        if (parameters != null) {
            UrlEncodedFormEntity uef = new UrlEncodedFormEntity(parameters, HTTP.UTF_8);
            post.setEntity(uef);
        }
        if (logger.isDebugEnabled()) {
            if (parameters != null) {
                logger.debug(" + Request parameters: ");
                for (NameValuePair param : parameters) {
                    logger.debug("   - " + param.getName() + " : " + param.getValue());
                }
            }
            logger.debug(" + Request headers: ");
            for (Header header : post.getAllHeaders()) {
                logger.debug("   - " + header.getName() + " : " + header.getValue());
            }
        }
        HttpResponse response = httpclient.execute(post);
        if (logger.isDebugEnabled()) {
            logger.debug(" + Response headers: ");
            for (Header header : response.getAllHeaders()) {
                logger.debug("   - " + header.getName() + " : " + header.getValue());
            }
        }
        logger.debug("***********************************************************************");
        return response;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HashMap(java.util.HashMap) Map(java.util.Map) CertificateException(java.security.cert.CertificateException)

Aggregations

HttpPost (org.apache.http.client.methods.HttpPost)531 HttpResponse (org.apache.http.HttpResponse)238 StringEntity (org.apache.http.entity.StringEntity)220 Test (org.junit.Test)153 IOException (java.io.IOException)143 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)107 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)99 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)90 ArrayList (java.util.ArrayList)86 NameValuePair (org.apache.http.NameValuePair)84 HttpEntity (org.apache.http.HttpEntity)83 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)68 HttpClient (org.apache.http.client.HttpClient)64 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)63 HttpGet (org.apache.http.client.methods.HttpGet)55 TestHttpClient (io.undertow.testutils.TestHttpClient)54 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JsonNode (com.fasterxml.jackson.databind.JsonNode)39 InputStream (java.io.InputStream)29