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();
}
}
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;
}
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());
}
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);
}
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);
}
}
Aggregations