下面列出了怎么用org.apache.http.entity.ContentProducer的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
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);
}
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);
}
}
/**
* 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);
}
/**
* 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);
}