本文整理汇总了Java中org.apache.http.entity.EntityTemplate类的典型用法代码示例。如果您正苦于以下问题:Java EntityTemplate类的具体用法?Java EntityTemplate怎么用?Java EntityTemplate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EntityTemplate类属于org.apache.http.entity包,在下文中一共展示了EntityTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: handle
import org.apache.http.entity.EntityTemplate; //导入依赖的package包/类
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
String target = request.getRequestLine().getUri();
String url = URLDecoder.decode(target, "UTF-8");
// Load the required resources
byte[] content;
if (injectedJavaObjectNameByURL.containsKey(url)) {
StringBuilder stringBuilder = new StringBuilder()
.append("define([], function() {\n")
.append(" return window.").append(injectedJavaObjectNameByURL.get(url)).append(";\n")
.append("});");
content = stringBuilder.toString().getBytes("UTF-8");
} else {
InputStream inputStream = findMatchingResource(url);
if (inputStream == null) {
throw new IOException("Unknown resource: " + url);
}
content = IOUtils.toByteArray(inputStream);
}
// Send the resource
EntityTemplate entity = new EntityTemplate(new SimpleContentProducer(content));
response.setEntity(entity);
}
示例10: 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);
}
示例11: getResponseByStream
import org.apache.http.entity.EntityTemplate; //导入依赖的package包/类
public String getResponseByStream(String url,String encode,String data,String sessionId)throws Exception{
httpPost = new HttpPost(url);
httpPost.setHeader("sessionId", sessionId);
StreamEntity se = new StreamEntity();
se.data=data;
se.encode=encode;
HttpEntity entity = new EntityTemplate(se);
httpPost.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = httpClient.execute(httpPost, responseHandler);
return content;
}
示例12: handle
import org.apache.http.entity.EntityTemplate; //导入依赖的package包/类
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
String contentType;
Integer code;
Uri uri = Uri.parse(request.getRequestLine().getUri());
String fileUri = URLDecoder.decode(uri.getLastPathSegment());
final byte[] r;
byte[] resp;
AssetManager mgr = context.getAssets();
try {
resp = Utility.loadInputStreamAsByte(mgr.open(fileUri));
contentType = Utility.getMimeTypeForFile(fileUri);
code = HttpStatus.OK.getCode();
} catch (IOException e){
resp = Utility.loadInputStreamAsByte(mgr.open("notfound.html"));
contentType = Utility.MIME_TYPES.get("html");
code = HttpStatus.NOT_FOUND.getCode();
}
r=resp;
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
outstream.write(r);
}
});
((EntityTemplate)entity).setContentType(contentType);
response.setStatusCode(code);
response.setEntity(entity);
}
示例13: handle
import org.apache.http.entity.EntityTemplate; //导入依赖的package包/类
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
String contentType;
Integer code;
Uri uri = Uri.parse(request.getRequestLine().getUri());
String folder;
List<String> s = uri.getPathSegments();
String path = File.separator;
for(int i=1;i<s.size()-1;i++) {
path+=s.get(i)+File.separator;
}
folder = path;
final File file = new File(Utility.BLACKHOLE_PATH +folder+s.get(s.size()-1));
final byte[] r;
byte[] resp;
if(file.exists()){
resp = Utility.loadFileAsByte(file.getAbsolutePath());
contentType = Utility.getMimeTypeForFile(file.getName());
code = HttpStatus.OK.getCode();
}else{
AssetManager mgr = context.getAssets();
resp = Utility.loadInputStreamAsByte(mgr.open("notfound.html"));
contentType = Utility.MIME_TYPES.get("html");
code = HttpStatus.NOT_FOUND.getCode();
}
r=resp;
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
outstream.write(r);
}
});
((EntityTemplate)entity).setContentType(contentType);
response.setStatusCode(code);
response.addHeader(HttpHeader.CONTENT_DISPOSITION, "attachment");
response.setEntity(entity);
}
示例14: forgeResponse
import org.apache.http.entity.EntityTemplate; //导入依赖的package包/类
public HttpEntity forgeResponse(){
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
outstream.write(response);
}
});
((EntityTemplate)entity).setContentType(contentType);
return entity;
}
示例15: 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);
}