本文整理汇总了Java中org.apache.commons.httpclient.methods.ByteArrayRequestEntity类的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayRequestEntity类的具体用法?Java ByteArrayRequestEntity怎么用?Java ByteArrayRequestEntity使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ByteArrayRequestEntity类属于org.apache.commons.httpclient.methods包,在下文中一共展示了ByteArrayRequestEntity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: post
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
@Override
public HttpResponse post(URL urlObj, byte[] payload, String userName, String password,
int timeout) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(urlObj.toString());
method.setRequestEntity(new ByteArrayRequestEntity(payload));
method.setRequestHeader("Content-type", "application/json");
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
client.getParams().setSoTimeout(1000 * timeout);
client.getParams().setConnectionManagerTimeout(1000 * timeout);
if (userName != null && password != null) {
setBasicAuthorization(method, userName, password);
}
try {
int response = client.executeMethod(method);
return new HttpResponse(response, method.getResponseBody());
} catch (IOException e) {
throw new RuntimeException("Failed to process post request URL: " + urlObj, e);
} finally {
method.releaseConnection();
}
}
示例2: createRequestEntity
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
/**
* Creates the request entity that makes up the POST message body.
*
* @param message message to be sent
* @param charset character set used for the message
*
* @return request entity that makes up the POST message body
*
* @throws SOAPClientException thrown if the message could not be marshalled
*/
protected RequestEntity createRequestEntity(Envelope message, Charset charset) throws SOAPClientException {
try {
Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(message);
ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(arrayOut, charset);
if (log.isDebugEnabled()) {
log.debug("Outbound SOAP message is:\n" + XMLHelper.prettyPrintXML(marshaller.marshall(message)));
}
XMLHelper.writeNode(marshaller.marshall(message), writer);
return new ByteArrayRequestEntity(arrayOut.toByteArray(), "text/xml");
} catch (MarshallingException e) {
throw new SOAPClientException("Unable to marshall SOAP envelope", e);
}
}
示例3: post
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
public HttpResponse post(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId,
final String relationCollectionName, final Object relationshipEntityId, final byte[] body, String contentType) throws IOException
{
RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName,
relationshipEntityId, null);
String url = endpoint.getUrl();
PostMethod req = new PostMethod(url.toString());
if (body != null)
{
if (contentType == null || contentType.isEmpty())
{
contentType = "application/octet-stream";
}
ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(body, contentType);
req.setRequestEntity(requestEntity);
}
return submitRequest(req, rq);
}
示例4: doPut
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
/**
* 处理Put请求
* @param url
* @param headers
* @param object
* @param property
* @return
*/
public static JSONObject doPut(String url, Map<String, String> headers,String jsonString) {
HttpClient httpClient = new HttpClient();
PutMethod putMethod = new PutMethod(url);
//设置header
setHeaders(putMethod,headers);
//设置put传递的json数据
if(jsonString!=null&&!"".equals(jsonString)){
putMethod.setRequestEntity(new ByteArrayRequestEntity(jsonString.getBytes()));
}
String responseStr = "";
try {
httpClient.executeMethod(putMethod);
responseStr = putMethod.getResponseBodyAsString();
} catch (Exception e) {
log.error(e);
responseStr="{status:0}";
}
return JSONObject.parseObject(responseStr);
}
示例5: addPrinterHW
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
@Override
public void addPrinterHW(final PrinterHWList printerHWList)
{
final byte[] data = beanEncoder.encode(printerHWList);
final URL url = getURL(PRTRestServiceConstants.PATH_AddPrinterHW);
final PostMethod httpPost = new PostMethod(url.toString());
final RequestEntity entity = new ByteArrayRequestEntity(data, beanEncoder.getContentType());
httpPost.setRequestEntity(entity);
int result = -1;
try
{
result = executeHttpPost(httpPost);
}
catch (final Exception e)
{
throw new PrintConnectionEndpointException("Cannot POST to " + url, e);
}
if (result != 200)
{
throw new PrintConnectionEndpointException("Error " + result + " while posting on " + url);
}
}
示例6: executeInternal
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
@Override
public ClientHttpResponse executeInternal(HttpHeaders headers, byte[] output) throws IOException {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
httpMethod.addRequestHeader(headerName, headerValue);
}
}
if (this.httpMethod instanceof EntityEnclosingMethod) {
EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) this.httpMethod;
RequestEntity requestEntity = new ByteArrayRequestEntity(output);
entityEnclosingMethod.setRequestEntity(requestEntity);
}
this.httpClient.executeMethod(this.httpMethod);
return new CommonsClientHttpResponse(this.httpMethod);
}
示例7: getSendByteArrayMethod
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
public PutMethod getSendByteArrayMethod(byte[] content, String service) throws Exception {
// Prepare HTTP put
//
String urlString = constructUrl(service);
log.logDebug(toString(), Messages.getString("SlaveServer.DEBUG_ConnectingTo", urlString)); //$NON-NLS-1$
PutMethod putMethod = new PutMethod(urlString);
// Request content will be retrieved directly from the input stream
//
RequestEntity entity = new ByteArrayRequestEntity(content);
putMethod.setRequestEntity(entity);
putMethod.setDoAuthentication(true);
putMethod.addRequestHeader(new Header("Content-Type", "text/xml;charset=" + Const.XML_ENCODING));
return putMethod;
}
示例8: getSendByteArrayMethod
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
public PostMethod getSendByteArrayMethod(byte[] content, String service) throws Exception {
// Prepare HTTP put
//
String urlString = constructUrl(service);
log.logDebug(BaseMessages.getString(PKG, "SlaveServer.DEBUG_ConnectingTo", urlString)); //$NON-NLS-1$
PostMethod postMethod = new PostMethod(urlString);
// Request content will be retrieved directly from the input stream
//
RequestEntity entity = new ByteArrayRequestEntity(content);
postMethod.setRequestEntity(entity);
postMethod.setDoAuthentication(true);
postMethod.addRequestHeader(new Header("Content-Type", "text/xml;charset=" + Const.XML_ENCODING));
return postMethod;
}
示例9: getSendByteArrayMethod
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
public PostMethod getSendByteArrayMethod(byte[] content, String service) throws Exception {
// Prepare HTTP put
//
String urlString = constructUrl(service);
if(log.isDebug()) log.logDebug(BaseMessages.getString(PKG, "SlaveServer.DEBUG_ConnectingTo", urlString)); //$NON-NLS-1$
PostMethod postMethod = new PostMethod(urlString);
// Request content will be retrieved directly from the input stream
//
RequestEntity entity = new ByteArrayRequestEntity(content);
postMethod.setRequestEntity(entity);
postMethod.setDoAuthentication(true);
postMethod.addRequestHeader(new Header("Content-Type", "text/xml;charset=" + Const.XML_ENCODING));
return postMethod;
}
示例10: setRequestAuthentication
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
@Override
public void setRequestAuthentication(HttpMethod method, byte[] message) throws IOException
{
if (method instanceof PostMethod)
{
// encrypt body
Pair<byte[], AlgorithmParameters> encrypted = encryptor.encrypt(KeyProvider.ALIAS_SOLR, null, message);
setRequestAlgorithmParameters(method, encrypted.getSecond());
((PostMethod) method).setRequestEntity(new ByteArrayRequestEntity(encrypted.getFirst(), "application/octet-stream"));
}
long requestTimestamp = System.currentTimeMillis();
// add MAC header
byte[] mac = macUtils.generateMAC(KeyProvider.ALIAS_SOLR, new MACInput(message, requestTimestamp, getLocalIPAddress()));
if (logger.isDebugEnabled())
{
logger.debug("Setting MAC " + mac + " on HTTP request " + method.getPath());
logger.debug("Setting timestamp " + requestTimestamp + " on HTTP request " + method.getPath());
}
if (overrideMAC)
{
mac[0] += (byte) 1;
}
setRequestMac(method, mac);
if (overrideTimestamp)
{
requestTimestamp += 60000;
}
// prevent replays
setRequestTimestamp(method, requestTimestamp);
}
示例11: put
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
/**
* Send a PUT request
* @param cluster the cluster definition
* @param path the path or URI
* @param headers the HTTP headers to include, <tt>Content-Type</tt> must be
* supplied
* @param content the content bytes
* @return a Response object with response detail
* @throws IOException
*/
public Response put(Cluster cluster, String path, Header[] headers,
byte[] content) throws IOException {
PutMethod method = new PutMethod();
try {
method.setRequestEntity(new ByteArrayRequestEntity(content));
int code = execute(cluster, method, headers, path);
headers = method.getResponseHeaders();
content = method.getResponseBody();
return new Response(code, headers, content);
} finally {
method.releaseConnection();
}
}
示例12: post
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
/**
* Send a POST request
* @param cluster the cluster definition
* @param path the path or URI
* @param headers the HTTP headers to include, <tt>Content-Type</tt> must be
* supplied
* @param content the content bytes
* @return a Response object with response detail
* @throws IOException
*/
public Response post(Cluster cluster, String path, Header[] headers,
byte[] content) throws IOException {
PostMethod method = new PostMethod();
try {
method.setRequestEntity(new ByteArrayRequestEntity(content));
int code = execute(cluster, method, headers, path);
headers = method.getResponseHeaders();
content = method.getResponseBody();
return new Response(code, headers, content);
} finally {
method.releaseConnection();
}
}
示例13: executePost
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
private PostMethod executePost(String uri, byte[] body) throws IOException {
HttpClient cli = new HttpClient();
final PostMethod post = new PostMethod(URI + ":" + source.port + uri);
post.setRequestEntity(new ByteArrayRequestEntity(body));
cli.executeMethod(post);
return post;
}
示例14: httpPost
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
protected static PostMethod httpPost(String path, String body) throws IOException {
LOG.info("Connecting to {}", url + path);
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url + path);
postMethod.addRequestHeader("Origin", url);
RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8"));
postMethod.setRequestEntity(entity);
httpClient.executeMethod(postMethod);
LOG.info("{} - {}", postMethod.getStatusCode(), postMethod.getStatusText());
return postMethod;
}
示例15: httpPut
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; //导入依赖的package包/类
protected static PutMethod httpPut(String path, String body) throws IOException {
LOG.info("Connecting to {}", url + path);
HttpClient httpClient = new HttpClient();
PutMethod putMethod = new PutMethod(url + path);
putMethod.addRequestHeader("Origin", url);
RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8"));
putMethod.setRequestEntity(entity);
httpClient.executeMethod(putMethod);
LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText());
return putMethod;
}