本文整理汇总了Java中org.apache.http.entity.EntityTemplate.setContentType方法的典型用法代码示例。如果您正苦于以下问题:Java EntityTemplate.setContentType方法的具体用法?Java EntityTemplate.setContentType怎么用?Java EntityTemplate.setContentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.EntityTemplate
的用法示例。
在下文中一共展示了EntityTemplate.setContentType方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
/**
* Send a HTTP POST request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPost(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPost(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例2: doPatch
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
/**
* Send a HTTP PATCH request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPatch(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPatch(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例3: doDeleteWithPayload
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
/**
* Send a HTTP DELETE request with entity body to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
boolean zip = false;
HttpUriRequest request = new HttpDeleteWithEntity(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
//check if content encoding required
if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
zip = true;
}
EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例4: doPut
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
/**
* Send a HTTP PUT request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPut(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPut(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例5: handle
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("GET") && !method.equals("HEAD")) {
throw new MethodNotSupportedException(method + " method not supported");
}
final EntityTemplate body = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write(mJSON);
writer.flush();
}
});
response.setStatusCode(HttpStatus.SC_OK);
body.setContentType("text/json; charset=UTF-8");
response.setEntity(body);
}
示例6: handle
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
public void handle(HttpRequest request, HttpResponse response, HttpContext arg2) throws HttpException, IOException {
if (request.getRequestLine().getMethod().equals("POST")) {
// Retrieve the POST content
HttpEntityEnclosingRequest post = (HttpEntityEnclosingRequest) request;
byte[] entityContent = EntityUtils.toByteArray(post.getEntity());
String content = new String(entityContent, Charset.forName("UTF-8"));
// Execute the request
final String json = RequestHandler.handle(content);
// Return the response
EntityTemplate body = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write(json);
writer.flush();
}
});
response.setStatusCode(HttpStatus.SC_OK);
body.setContentType("application/json; charset=UTF-8");
response.setEntity(body);
}
}
示例7: doPost
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
/**
* Send a HTTP POST request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPost(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPost(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes(Charset.defaultCharset()));
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例8: doPut
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
/**
* Send a HTTP PUT request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPut(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPut(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes(Charset.defaultCharset()));
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例9: doOptions
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
/**
* Send a HTTP OPTIONS request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doOptions(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpOptions(url);
setHeaders(headers, request);
if(payload != null) {
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
}
return client.execute(request);
}
示例10: writeContent
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
private void writeContent(HttpRequest request, HttpResponse response) {
// Check for edge cases as stated in the HTTP specs
if ("HEAD".equals(request.getRequestLine().getMethod()) ||
statusCode == HttpStatus.SC_NO_CONTENT ||
statusCode == HttpStatus.SC_RESET_CONTENT ||
statusCode == HttpStatus.SC_NOT_MODIFIED) {
return;
}
EntityTemplate body = createEntity();
body.setContentType(contentType);
response.setEntity(body);
}
示例11: postDocsToPipeline
import org.apache.http.entity.EntityTemplate; //导入方法依赖的package包/类
public void postDocsToPipeline(String hostAndPort, String pipelinePath, List docs, int requestId, String contentType) throws Exception {
FusionSession fusionSession = getSession(hostAndPort, requestId);
String postUrl = hostAndPort + pipelinePath;
if (postUrl.indexOf("?") != -1) {
postUrl += "&echo=false";
} else {
postUrl += "?echo=false";
}
HttpPost postRequest = new HttpPost(postUrl);
ContentProducer cp = newContentProducer(contentType, docs);
EntityTemplate et = new EntityTemplate(cp);
et.setContentType(contentType);
et.setContentEncoding(StandardCharsets.UTF_8.name());
postRequest.setEntity(et);
HttpEntity entity = null;
CloseableHttpResponse response = null;
try {
HttpClientContext context = null;
if (isKerberos) {
response = httpClient.execute(postRequest);
} else {
context = HttpClientContext.create();
if (cookieStore != null) {
context.setCookieStore(cookieStore);
}
response = httpClient.execute(postRequest, context);
}
entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 401) {
// unauth'd - session probably expired? retry to establish
log.warn("Unauthorized error (401) when trying to send request " + requestId +
" to Fusion at " + hostAndPort + ", will re-try to establish session");
// re-establish the session and re-try the request
EntityUtils.consumeQuietly(entity);
entity = null;
response.close();
synchronized (this) {
fusionSession = resetSession(hostAndPort);
if (fusionSession == null)
throw new IllegalStateException("After re-establishing session when processing request " +
requestId + ", hostAndPort " + hostAndPort + " is no longer active! Try another hostAndPort.");
}
log.info("Going to re-try request " + requestId + " after session re-established with " + hostAndPort);
if (isKerberos) {
response = httpClient.execute(postRequest);
} else {
response = httpClient.execute(postRequest, context);
}
entity = response.getEntity();
statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 204) {
log.info("Re-try request " + requestId + " after session timeout succeeded for: " + hostAndPort);
} else {
raiseFusionServerException(hostAndPort, entity, statusCode, response, requestId);
}
} else if (statusCode != 200 && statusCode != 204) {
raiseFusionServerException(hostAndPort, entity, statusCode, response, requestId);
} else {
// OK!
if (fusionSession != null && fusionSession.docsSentMeter != null)
fusionSession.docsSentMeter.mark(docs.size());
}
} finally {
EntityUtils.consumeQuietly(entity);
if (response != null) response.close();
}
}