本文整理汇总了Java中org.apache.http.client.entity.EntityBuilder.setContentType方法的典型用法代码示例。如果您正苦于以下问题:Java EntityBuilder.setContentType方法的具体用法?Java EntityBuilder.setContentType怎么用?Java EntityBuilder.setContentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.entity.EntityBuilder
的用法示例。
在下文中一共展示了EntityBuilder.setContentType方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateSuspect
import org.apache.http.client.entity.EntityBuilder; //导入方法依赖的package包/类
public void testCreateSuspect() throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
StringBuilder url = new StringBuilder(BASE_URL);
url.append("suspect/");
HttpPost httpPost = new HttpPost(url.toString());
EntityBuilder entityBuilder = EntityBuilder.create();
entityBuilder.setContentType(ContentType.APPLICATION_JSON);
Suspect aaron = createMockSuspect("982c4a67-bb39-41f9-9872-88356822b7ad", "Aaron");
JsonHelper jsonHelper = new JsonHelper();
String jsonRequest = jsonHelper.convertToJson(aaron);
entityBuilder.setText(jsonRequest);
httpPost.setEntity(entityBuilder.build());
CloseableHttpResponse response = httpclient.execute(httpPost);
Assert.assertTrue(true);
}
示例2: sendFilePostRequest
import org.apache.http.client.entity.EntityBuilder; //导入方法依赖的package包/类
/**
* Executes FILE (binary) HTTP POST request based on configuration
*
* @param config
* DPU configuration
* @param client
* HTTP client used to execute request
* @return HTTP response
* @throws Exception
* if request execution fails
*/
public CloseableHttpResponse sendFilePostRequest(HttpRequestConfig_V1 config, File file, CloseableHttpClient client) throws Exception {
CloseableHttpResponse response = null;
try {
URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
uriBuilder.setPath(uriBuilder.getPath());
HttpPost request = new HttpPost(uriBuilder.build().normalize());
// if (rdfConfig.isUseAuthentication()) {
// addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
// }
EntityBuilder builder = EntityBuilder.create();
builder.setContentEncoding(config.getCharset());
//ContentType contentType = ContentType.DEFAULT_BINARY;
ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
builder.setFile(file);
builder.setContentType(contentType);
HttpEntity entity = builder.build();
request.setEntity(entity);
request.addHeader("Content-Type", contentType.toString());
LOG.info("Request: {}", request.toString());
response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
//response = client.execute(request);
checkHttpResponseStatus(response);
} catch (URISyntaxException | IllegalStateException | IOException ex) {
String errorMsg = String.format("Failed to execute HTTP file POST request to URL %s", config.getRequestURL());
LOG.error(errorMsg, ex);
throw new Exception(errorMsg, ex);
}
return response;
}
示例3: sendFilePutRequest
import org.apache.http.client.entity.EntityBuilder; //导入方法依赖的package包/类
/**
* Executes FILE (binary) HTTP PUT request based on configuration
*
* @param config
* DPU configuration
* @param client
* HTTP client used to execute request
* @return HTTP response
* @throws Exception
* if request execution fails
*/
public CloseableHttpResponse sendFilePutRequest(HttpRequestConfig_V1 config, File file, CloseableHttpClient client) throws Exception {
CloseableHttpResponse response = null;
try {
URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
uriBuilder.setPath(uriBuilder.getPath());
HttpPut request = new HttpPut(uriBuilder.build().normalize());
// if (rdfConfig.isUseAuthentication()) {
// addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
// }
EntityBuilder builder = EntityBuilder.create();
builder.setContentEncoding(config.getCharset());
//ContentType contentType = ContentType.DEFAULT_BINARY;
ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
builder.setFile(file);
builder.setContentType(contentType);
HttpEntity entity = builder.build();
request.setEntity(entity);
request.addHeader("Content-Type", contentType.toString());
LOG.info("Request: {}", request.toString());
response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
//response = client.execute(request);
checkHttpResponseStatus(response);
} catch (URISyntaxException | IllegalStateException | IOException ex) {
String errorMsg = String.format("Failed to execute HTTP file PUT request to URL %s", config.getRequestURL());
LOG.error(errorMsg, ex);
throw new Exception(errorMsg, ex);
}
return response;
}
示例4: sendRawDataPostRequest
import org.apache.http.client.entity.EntityBuilder; //导入方法依赖的package包/类
/**
* Executes RAW data (text data) HTTP POST request based on configuration
*
* @param config
* DPU configuration
* @param client
* HTTP client used to execute request
* @return HTTP response
* @throws Exception
* if request execution fails
*/
public CloseableHttpResponse sendRawDataPostRequest(HttpRequestConfig_V1 config, CloseableHttpClient client) throws Exception {
CloseableHttpResponse response = null;
try {
URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
uriBuilder.setPath(uriBuilder.getPath());
HttpPost request = new HttpPost(uriBuilder.build().normalize());
// if (rdfConfig.isUseAuthentication()) {
// addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
// }
EntityBuilder builder = EntityBuilder.create();
builder.setContentEncoding(config.getCharset());
ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
builder.setText(config.getRawRequestBody());
builder.setContentType(contentType);
HttpEntity entity = builder.build();
request.setEntity(entity);
request.addHeader("Content-Type", contentType.toString());
response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
// response = client.execute(request);
checkHttpResponseStatus(response);
} catch (URISyntaxException | IllegalStateException | IOException ex) {
String errorMsg = String.format("Failed to execute HTTP raw POST request to URL %s", config.getRequestURL());
LOG.error(errorMsg);
throw new Exception(errorMsg, ex);
}
return response;
}
示例5: sendRawDataPutRequest
import org.apache.http.client.entity.EntityBuilder; //导入方法依赖的package包/类
/**
* Executes RAW data (text data) HTTP PUT request based on configuration
*
* @param config
* DPU configuration
* @param client
* HTTP client used to execute request
* @return HTTP response
* @throws Exception
* if request execution fails
*/
public CloseableHttpResponse sendRawDataPutRequest(HttpRequestConfig_V1 config, CloseableHttpClient client) throws Exception {
CloseableHttpResponse response = null;
try {
URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
uriBuilder.setPath(uriBuilder.getPath());
HttpPut request = new HttpPut(uriBuilder.build().normalize());
// if (rdfConfig.isUseAuthentication()) {
// addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
// }
EntityBuilder builder = EntityBuilder.create();
builder.setContentEncoding(config.getCharset());
ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
builder.setText(config.getRawRequestBody());
builder.setContentType(contentType);
HttpEntity entity = builder.build();
request.setEntity(entity);
request.addHeader("Content-Type", contentType.toString());
response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
// response = client.execute(request);
checkHttpResponseStatus(response);
} catch (URISyntaxException | IllegalStateException | IOException ex) {
String errorMsg = String.format("Failed to execute HTTP raw PUT request to URL %s", config.getRequestURL());
LOG.error(errorMsg);
throw new Exception(errorMsg, ex);
}
return response;
}
示例6: communicate
import org.apache.http.client.entity.EntityBuilder; //导入方法依赖的package包/类
@Override
public void communicate() {
final String url = "https://www.googleapis.com/urlshortener/v1/url?key=%s";
InputStream resstream = null;
JsonReader jsonReader1 = null;
try {
setCurrent();
// create the get request.
final HttpPost httppost = new HttpPost(String.format(url, this.key));
final EntityBuilder builder = EntityBuilder.create();
final String reqjson = Client.gson.toJson(new GooglRequest(this.shortreq.getLongURL()));
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(reqjson);
httppost.setEntity(builder.build());
// execute request
final HttpResponse response = Downloader.downloader.client.execute(httppost);
if (response.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
final HttpEntity resEntity = response.getEntity();
if (resEntity!=null) {
resstream = resEntity.getContent();
this.result = Client.gson.<GooglResult> fromJson(jsonReader1 = new JsonReader(new InputStreamReader(resstream, Charsets.UTF_8)), GooglResult.class);
onDone(new CommunicateResponse(true, null));
return;
}
} else {
onDone(new CommunicateResponse(false, new IOException("Bad Response")));
return;
}
} catch (final Exception e) {
onDone(new CommunicateResponse(false, e));
return;
} finally {
unsetCurrent();
IOUtils.closeQuietly(resstream);
IOUtils.closeQuietly(jsonReader1);
}
onDone(new CommunicateResponse(false, null));
return;
}