AsyncHttpClient.BoundRequestBuilder getJsonUrlRequest() { AsyncHttpClient.BoundRequestBuilder requestBuilder = buildSimpleRequest(packId); for (Map.Entry<String, String> param : this.stringParams.entrySet()) { if (this.fileParams.isEmpty()) { requestBuilder.addFormParam(param.getKey(), param.getValue()); } else { requestBuilder.addQueryParam(param.getKey(), param.getValue()); } } for (Map.Entry<String, FilePart> param : this.fileParams.entrySet()) { requestBuilder.addBodyPart(param.getValue()); } return requestBuilder; }
@Override public DetailedResponse getDetailedResponse(final ResponseProgressListener listener) throws Exception { AsyncHttpClient.BoundRequestBuilder requestBuilder = buildDetailedRequest(packId); for (Map.Entry<String, String> param : this.stringParams.entrySet()) { if (this.fileParams.isEmpty()) { requestBuilder.addFormParam(param.getKey(), param.getValue()); } else { requestBuilder.addQueryParam(param.getKey(), param.getValue()); } } for (Map.Entry<String, FilePart> param : this.fileParams.entrySet()) { requestBuilder.addBodyPart(param.getValue()); } return executeDetailedRequest(requestBuilder.build(), listener); }
@Override public SimpleResponse getUpdateResponse() throws Exception { AsyncHttpClient.BoundRequestBuilder requestBuilder = buildUpdateRequest(); for (Map.Entry<String, String> param : this.stringParams.entrySet()) { requestBuilder.addBodyPart( new StringPart(param.getKey(), param.getValue(), getCharset().name())); } for (Map.Entry<String, FilePart> param : this.fileParams.entrySet()) { requestBuilder.addBodyPart(param.getValue()); } return executeSimpleRequest(requestBuilder.build()); }
builder.addBodyPart(new FilePart(this.fileParams[i].paramName, this.fileParams[i].file, MimeTypes.getMimeType(this.fileParams[i].file.getName()), Charset.forName(encoding))); Part part = new ByteArrayPart(key, v.toString().getBytes(encoding), "text/plain", Charset.forName(encoding), null); builder.addBodyPart(part); builder.addBodyPart(part);
builder.setBodyEncoding("UTF-8"); FilePart part = new FilePart("audio", tmpFile, "", ""); builder.addBodyPart(part); builder.addBodyPart(new StringPart("from", "1")) .execute(new AsyncCompletionHandler<Response>() {
private <T> ListenableFuture<T> privatePost(final String url, HttpOption option, AsyncHandler<T> handler) throws IOException { AsyncHttpClient.BoundRequestBuilder builder = getClient().preparePost(url); if (option != null) { Map<String, String> postFormData = option.getPostFormData(); Map<String, String> headers = option.getHeaders(); String body = option.getPostBodyData(); ProxyServer proxy = option.getProxy(); if (headers != null && !headers.isEmpty()) { for (Map.Entry<String, String> entry : headers.entrySet()) { builder.addHeader(entry.getKey(), entry.getValue()); } } if (postFormData != null && !postFormData.isEmpty()) { for (Map.Entry<String, String> entry : postFormData.entrySet()) { builder.addBodyPart(new StringPart(entry.getKey(), entry.getValue())); } } if (!Strings.isNullOrEmpty(body)) { builder.setBody(body); } if (proxy != null) { builder.setProxyServer(proxy); } } Request request = builder.build(); return new GuavaListenableFuture<T>(getClient().executeRequest(request, new AsyncClientHandler<T>(handler))); }
@Test(groups = { "standalone", "default_provider", "async" }) public void asyncDoPostMultiPartTest() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(null)) { final CountDownLatch l = new CountDownLatch(1); Part p = new StringPart("foo", "bar"); client.preparePost(getTargetUrl()).addBodyPart(p).execute(new AsyncCompletionHandlerAdapter() { @Override public Response onCompleted(Response response) throws Exception { try { String xContentType = response.getHeader("X-Content-Type"); String boundary = xContentType.substring((xContentType.indexOf("boundary") + "boundary".length() + 1)); String s = response.getResponseBodyExcerpt(boundary.length() + "--".length()).substring("--".length()); assertEquals(boundary, s); } finally { l.countDown(); } return response; } }).get(); if (!l.await(TIMEOUT, TimeUnit.SECONDS)) { Assert.fail("Timeout out"); } } }
<T> ListenableFuture<T> privatePost(final String url, Map<String, String> params, HttpOption option, String charset, AsyncHandler<T> handler) throws IOException { AsyncHttpClient.BoundRequestBuilder builder = getClient().preparePost(url); for (Map.Entry<String, String> entry : params.entrySet()) { StringPart part = new StringPart(entry.getKey(), entry.getValue(), null, Charset.forName(charset)); builder.addBodyPart(part); } if (option != null) { // 设置header for (Map.Entry<String, String> entry : option.getHeaders().entrySet()) { builder.addHeader(entry.getKey(), entry.getValue()); } // 设置代理 if (option.getProxy() != null) { builder.setProxyServer(option.getProxy()); } } Request request = builder.build(); return new GuavaListenableFuture<T>(getClient().executeRequest(request, new AsyncClientHandler<T>(handler))); }
public WrappedRequestBuilder addBodyPart(Part part) { _delegate.addBodyPart(part); return this; }
@Test(groups = { "standalone", "default_provider" }, enabled = true) public void testUnauthorizedWhileUploading() throws Exception { byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes(UTF_16); long repeats = (1024 * 1024 / bytes.length) + 1; File largeFile = FilePartLargeFileTest.createTempFile(bytes, (int) repeats); try (AsyncHttpClient client = getAsyncHttpClient(null)) { BoundRequestBuilder rb = client.preparePut(getTargetUrl()); rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", UTF_8)); Response response = rb.execute().get(); Assert.assertEquals(401, response.getStatusCode()); } } }
@Test(groups = { "standalone", "default_provider" }, enabled = true) public void testPutImageFile() throws Exception { File largeFile = getTestFile(); AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setRequestTimeout(100 * 6000).build(); try (AsyncHttpClient client = getAsyncHttpClient(config)) { BoundRequestBuilder rb = client.preparePut(getTargetUrl()); rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", UTF_8)); Response response = rb.execute().get(); Assert.assertEquals(200, response.getStatusCode()); } }
@Test(groups = { "standalone", "default_provider" }, enabled = true) public void testPutLargeTextFile() throws Exception { byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes(UTF_16); long repeats = (1024 * 1024 / bytes.length) + 1; File largeFile = createTempFile(bytes, (int) repeats); try (AsyncHttpClient client = getAsyncHttpClient(null)) { BoundRequestBuilder rb = client.preparePut(getTargetUrl()); rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", UTF_8)); Response response = rb.execute().get(); Assert.assertEquals(200, response.getStatusCode()); } }
public WrappedRequestBuilder addBodyPart(Part part) { _delegate.addBodyPart(part); return this; }