Java源码示例:org.apache.http.entity.EntityTemplate
示例1
/**
* 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);
}
示例2
/**
* Sends 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);
HTTPUtils.setHTTPHeaders(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 httpclient.execute(request);
}
示例3
/**
* 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);
}
示例4
/**
* 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);
}
示例5
/**
* 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);
}
示例6
/**
* 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);
}
示例7
/**
* Sends 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);
HTTPUtils.setHTTPHeaders(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 httpclient.execute(request);
}
示例8
/**
* Prepares a HttpUriRequest to be sent.
*
* @param headers HTTP headers to be set as a MAP <Header name, Header value>
* @param payload Final payload to be sent
* @param contentType Content-type (i.e application/json)
* @param request HttpUriRequest request to be prepared
*/
private void prepareRequest(Map<String, String> headers, final String payload, String contentType,
HttpUriRequest request) {
HTTPUtils.setHTTPHeaders(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);
}
示例9
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);
}
示例10
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);
}
}
示例11
/**
* 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);
}
示例12
/**
* Sends a simple SOAP message and returns the response.
*
* @param url HTTP service url to send message
* @param payload final payload as a String to be sent. Message should adhere to SOAP principles
* @param action SOAP Action to set. Specify with "urn:"
* @param headers HTTP headers to be set as a MAP <Header name, Header value>
* @return HTTP response after invocation
* @throws IOException in case of an transport error invoking service
*/
public HttpResponse sendSimpleSOAPMessage(String url, final String payload, String action,
final Map<String, String> headers) throws IOException {
HttpUriRequest request = new HttpPost(url);
HTTPUtils.setHTTPHeader(SOAP_ACTION, action, request);
if(headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
HTTPUtils.setHTTPHeader(header.getKey(),header.getValue(), request);
}
}
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
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(HttpConstants.MEDIA_TYPE_TEXT_XML);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return this.httpclient.execute(request);
}