@Test public void buildEntity() { HttpEntity httpEntity = entityBuilder .setBody("testBody") .buildEntity(); assertThat(httpEntity, instanceOf(StringEntity.class)); StringEntity stringEntity = (StringEntity) httpEntity; assertNull(stringEntity.getContentType()); }
public HttpEntity buildEntity() { AbstractHttpEntity httpEntity = null; if (!StringUtils.isEmpty(formParams)) { List<? extends NameValuePair> list; list = getNameValuePairs(formParams, !Boolean.parseBoolean(this.formParamsAreURLEncoded), HttpClientInputs.FORM_PARAMS, HttpClientInputs.FORM_PARAMS_ARE_URLENCODED); Charset charset = contentType != null ? contentType.getCharset() : null; httpEntity = new UrlEncodedFormEntity(list, charset); } else if (!StringUtils.isEmpty(body)) { httpEntity = new StringEntity(body, contentType); } else if (!StringUtils.isEmpty(filePath)) { File file = new File(filePath); if (!file.exists()) { throw new IllegalArgumentException("file set by input '" + HttpClientInputs.SOURCE_FILE + "' does not exist:" + filePath); } httpEntity = new FileEntity(file, contentType); } if (httpEntity != null) { if (!StringUtils.isEmpty(chunkedRequestEntity)) { httpEntity.setChunked(Boolean.parseBoolean(chunkedRequestEntity)); } return httpEntity; } if (!StringUtils.isEmpty(multipartBodies) || !StringUtils.isEmpty(multipartFiles)) { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); if (!StringUtils.isEmpty(multipartBodies)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartBodies, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_BODIES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType bodiesCT = ContentType.parse(multipartBodiesContentType); for (NameValuePair nameValuePair : list) { multipartEntityBuilder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(), bodiesCT); } } if (!StringUtils.isEmpty(multipartFiles)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartFiles, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_FILES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType filesCT = ContentType.parse(multipartFilesContentType); for (NameValuePair nameValuePair : list) { File file = new File(nameValuePair.getValue()); multipartEntityBuilder.addBinaryBody(nameValuePair.getName(), file, filesCT, file.getName()); } } return multipartEntityBuilder.build(); } return null; }
EntityBuilder { public HttpEntity buildEntity() { AbstractHttpEntity httpEntity = null; if (!StringUtils.isEmpty(formParams)) { List<? extends NameValuePair> list; list = getNameValuePairs(formParams, !Boolean.parseBoolean(this.formParamsAreURLEncoded), HttpClientInputs.FORM_PARAMS, HttpClientInputs.FORM_PARAMS_ARE_URLENCODED); Charset charset = contentType != null ? contentType.getCharset() : null; httpEntity = new UrlEncodedFormEntity(list, charset); } else if (!StringUtils.isEmpty(body)) { httpEntity = new StringEntity(body, contentType); } else if (!StringUtils.isEmpty(filePath)) { File file = new File(filePath); if (!file.exists()) { throw new IllegalArgumentException("file set by input '" + HttpClientInputs.SOURCE_FILE + "' does not exist:" + filePath); } httpEntity = new FileEntity(file, contentType); } if (httpEntity != null) { if (!StringUtils.isEmpty(chunkedRequestEntity)) { httpEntity.setChunked(Boolean.parseBoolean(chunkedRequestEntity)); } return httpEntity; } if (!StringUtils.isEmpty(multipartBodies) || !StringUtils.isEmpty(multipartFiles)) { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); if (!StringUtils.isEmpty(multipartBodies)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartBodies, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_BODIES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType bodiesCT = ContentType.parse(multipartBodiesContentType); for (NameValuePair nameValuePair : list) { multipartEntityBuilder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(), bodiesCT); } } if (!StringUtils.isEmpty(multipartFiles)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartFiles, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_FILES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType filesCT = ContentType.parse(multipartFilesContentType); for (NameValuePair nameValuePair : list) { File file = new File(nameValuePair.getValue()); multipartEntityBuilder.addBinaryBody(nameValuePair.getName(), file, filesCT, file.getName()); } } return multipartEntityBuilder.build(); } return null; } }
EntityBuilder { public HttpEntity buildEntity() { AbstractHttpEntity httpEntity = null; if (!StringUtils.isEmpty(formParams)) { List<? extends NameValuePair> list; list = getNameValuePairs(formParams, !Boolean.parseBoolean(this.formParamsAreURLEncoded), HttpClientInputs.FORM_PARAMS, HttpClientInputs.FORM_PARAMS_ARE_URLENCODED); Charset charset = contentType != null ? contentType.getCharset() : null; httpEntity = new UrlEncodedFormEntity(list, charset); } else if (!StringUtils.isEmpty(body)) { httpEntity = new StringEntity(body, contentType); } else if (!StringUtils.isEmpty(filePath)) { File file = new File(filePath); if (!file.exists()) { throw new IllegalArgumentException("file set by input '" + HttpClientInputs.SOURCE_FILE + "' does not exist:" + filePath); } httpEntity = new FileEntity(file, contentType); } if (httpEntity != null) { if (!StringUtils.isEmpty(chunkedRequestEntity)) { httpEntity.setChunked(Boolean.parseBoolean(chunkedRequestEntity)); } return httpEntity; } if (!StringUtils.isEmpty(multipartBodies) || !StringUtils.isEmpty(multipartFiles)) { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); if (!StringUtils.isEmpty(multipartBodies)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartBodies, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_BODIES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType bodiesCT = ContentType.parse(multipartBodiesContentType); for (NameValuePair nameValuePair : list) { multipartEntityBuilder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(), bodiesCT); } } if (!StringUtils.isEmpty(multipartFiles)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartFiles, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_FILES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType filesCT = ContentType.parse(multipartFilesContentType); for (NameValuePair nameValuePair : list) { File file = new File(nameValuePair.getValue()); multipartEntityBuilder.addBinaryBody(nameValuePair.getName(), file, filesCT, file.getName()); } } return multipartEntityBuilder.build(); } return null; }  }
EntityBuilder { public HttpEntity buildEntity() { AbstractHttpEntity httpEntity = null; if (!StringUtils.isEmpty(formParams)) { List<? extends NameValuePair> list; list = getNameValuePairs(formParams, !Boolean.parseBoolean(this.formParamsAreURLEncoded), HttpClientInputs.FORM_PARAMS, HttpClientInputs.FORM_PARAMS_ARE_URLENCODED); Charset charset = contentType != null ? contentType.getCharset() : null; httpEntity = new UrlEncodedFormEntity(list, charset); } else if (!StringUtils.isEmpty(body)) { httpEntity = new StringEntity(body, contentType); } else if (!StringUtils.isEmpty(filePath)) { File file = new File(filePath); if (!file.exists()) { throw new IllegalArgumentException("file set by input '" + HttpClientInputs.SOURCE_FILE + "' does not exist:" + filePath); } httpEntity = new FileEntity(file, contentType); } if (httpEntity != null) { if (!StringUtils.isEmpty(chunkedRequestEntity)) { httpEntity.setChunked(Boolean.parseBoolean(chunkedRequestEntity)); } return httpEntity; } if (!StringUtils.isEmpty(multipartBodies) || !StringUtils.isEmpty(multipartFiles)) { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); if (!StringUtils.isEmpty(multipartBodies)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartBodies, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_BODIES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType bodiesCT = ContentType.parse(multipartBodiesContentType); for (NameValuePair nameValuePair : list) { multipartEntityBuilder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(), bodiesCT); } } if (!StringUtils.isEmpty(multipartFiles)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartFiles, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_FILES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType filesCT = ContentType.parse(multipartFilesContentType); for (NameValuePair nameValuePair : list) { File file = new File(nameValuePair.getValue()); multipartEntityBuilder.addBinaryBody(nameValuePair.getName(), file, filesCT, file.getName()); } } return multipartEntityBuilder.build(); } return null; }  EntityBuilder setBody(String body); EntityBuilder setFilePath(String filePath); EntityBuilder setContentType(ContentType contentType); EntityBuilder setFormParams(String formParams); EntityBuilder setFormParamsAreURLEncoded(String formParamsAreURLEncoded); EntityBuilder setMultipartBodies(String multipartBodies); EntityBuilder setMultipartFiles(String multipartFiles); EntityBuilder setMultipartValuesAreURLEncoded(String multipartValuesAreURLEncoded); EntityBuilder setMultipartBodiesContentType(String multipartBodiesContentType); EntityBuilder setMultipartFilesContentType(String multipartFilesContentType); EntityBuilder setChunkedRequestEntity(String chunkedRequestEntity); HttpEntity buildEntity(); }
EntityBuilder { public HttpEntity buildEntity() { AbstractHttpEntity httpEntity = null; if (!StringUtils.isEmpty(formParams)) { List<? extends NameValuePair> list; list = getNameValuePairs(formParams, !Boolean.parseBoolean(this.formParamsAreURLEncoded), HttpClientInputs.FORM_PARAMS, HttpClientInputs.FORM_PARAMS_ARE_URLENCODED); Charset charset = contentType != null ? contentType.getCharset() : null; httpEntity = new UrlEncodedFormEntity(list, charset); } else if (!StringUtils.isEmpty(body)) { httpEntity = new StringEntity(body, contentType); } else if (!StringUtils.isEmpty(filePath)) { File file = new File(filePath); if (!file.exists()) { throw new IllegalArgumentException("file set by input '" + HttpClientInputs.SOURCE_FILE + "' does not exist:" + filePath); } httpEntity = new FileEntity(file, contentType); } if (httpEntity != null) { if (!StringUtils.isEmpty(chunkedRequestEntity)) { httpEntity.setChunked(Boolean.parseBoolean(chunkedRequestEntity)); } return httpEntity; } if (!StringUtils.isEmpty(multipartBodies) || !StringUtils.isEmpty(multipartFiles)) { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); if (!StringUtils.isEmpty(multipartBodies)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartBodies, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_BODIES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType bodiesCT = ContentType.parse(multipartBodiesContentType); for (NameValuePair nameValuePair : list) { multipartEntityBuilder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(), bodiesCT); } } if (!StringUtils.isEmpty(multipartFiles)) { List<? extends NameValuePair> list; list = getNameValuePairs(multipartFiles, !Boolean.parseBoolean(this.multipartValuesAreURLEncoded), HttpClientInputs.MULTIPART_FILES, HttpClientInputs.MULTIPART_VALUES_ARE_URLENCODED); ContentType filesCT = ContentType.parse(multipartFilesContentType); for (NameValuePair nameValuePair : list) { File file = new File(nameValuePair.getValue()); multipartEntityBuilder.addBinaryBody(nameValuePair.getName(), file, filesCT, file.getName()); } } return multipartEntityBuilder.build(); } return null; }  EntityBuilder setBody(String body); EntityBuilder setFilePath(String filePath); EntityBuilder setContentType(ContentType contentType); EntityBuilder setFormParams(String formParams); EntityBuilder setFormParamsAreURLEncoded(String formParamsAreURLEncoded); EntityBuilder setMultipartBodies(String multipartBodies); EntityBuilder setMultipartFiles(String multipartFiles); EntityBuilder setMultipartValuesAreURLEncoded(String multipartValuesAreURLEncoded); EntityBuilder setMultipartBodiesContentType(String multipartBodiesContentType); EntityBuilder setMultipartFilesContentType(String multipartFilesContentType); EntityBuilder setChunkedRequestEntity(String chunkedRequestEntity); HttpEntity buildEntity();  }