本文整理匯總了Java中org.apache.commons.httpclient.methods.InputStreamRequestEntity類的典型用法代碼示例。如果您正苦於以下問題:Java InputStreamRequestEntity類的具體用法?Java InputStreamRequestEntity怎麽用?Java InputStreamRequestEntity使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
InputStreamRequestEntity類屬於org.apache.commons.httpclient.methods包,在下文中一共展示了InputStreamRequestEntity類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: ChunkEncodedPost <file>");
System.out.println("<file> - full path to a file to be posted");
System.exit(1);
}
HttpClient client = new HttpClient();
PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");
File file = new File(args[0]);
httppost.setRequestEntity(new InputStreamRequestEntity(
new FileInputStream(file), file.length()));
try {
client.executeMethod(httppost);
if (httppost.getStatusCode() == HttpStatus.SC_OK) {
System.out.println(httppost.getResponseBodyAsString());
} else {
System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
}
} finally {
httppost.releaseConnection();
}
}
示例2: handleMultipartPost
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
/**
* Sets up the given {@link org.apache.commons.httpclient.methods.PostMethod} to send the same multipart POST data
* as was sent in the given {@link HttpServletRequest}
*
* @param postMethodProxyRequest The {@link org.apache.commons.httpclient.methods.PostMethod} that we are configuring to send a
* multipart POST request
* @param httpServletRequest The {@link HttpServletRequest} that contains the multipart
* POST data to be sent via the {@link org.apache.commons.httpclient.methods.PostMethod}
*/
@SuppressWarnings("unchecked")
public static void handleMultipartPost(
EntityEnclosingMethod postMethodProxyRequest,
HttpServletRequest httpServletRequest,
DiskFileItemFactory diskFileItemFactory)
throws ServletException {
// TODO: this function doesn't set any history data
try {
// just pass back the binary data
InputStreamRequestEntity ire = new InputStreamRequestEntity(httpServletRequest.getInputStream());
postMethodProxyRequest.setRequestEntity(ire);
postMethodProxyRequest.setRequestHeader(STRING_CONTENT_TYPE_HEADER_NAME, httpServletRequest.getHeader(STRING_CONTENT_TYPE_HEADER_NAME));
} catch (Exception e) {
throw new ServletException(e);
}
}
示例3: testEnclosedEntityAutoLength
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public void testEnclosedEntityAutoLength() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
assertEquals(200, method.getStatusCode());
String body = method.getResponseBodyAsString();
assertEquals(inputstr, body);
assertNull(method.getRequestHeader("Transfer-Encoding"));
assertNotNull(method.getRequestHeader("Content-Length"));
assertEquals(input.length, Integer.parseInt(
method.getRequestHeader("Content-Length").getValue()));
} finally {
method.releaseConnection();
}
}
示例4: testEnclosedEntityExplicitLength
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public void testEnclosedEntityExplicitLength() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, 14);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
assertEquals(200, method.getStatusCode());
String body = method.getResponseBodyAsString();
assertEquals("This is a test", body);
assertNull(method.getRequestHeader("Transfer-Encoding"));
assertNotNull(method.getRequestHeader("Content-Length"));
assertEquals(14, Integer.parseInt(
method.getRequestHeader("Content-Length").getValue()));
} finally {
method.releaseConnection();
}
}
示例5: testEnclosedEntityChunked
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public void testEnclosedEntityChunked() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
method.setContentChunked(true);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
assertEquals(200, method.getStatusCode());
String body = method.getResponseBodyAsString();
assertEquals(inputstr, body);
assertNotNull(method.getRequestHeader("Transfer-Encoding"));
assertNull(method.getRequestHeader("Content-Length"));
} finally {
method.releaseConnection();
}
}
示例6: testEnclosedEntityChunkedHTTP1_0
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public void testEnclosedEntityChunkedHTTP1_0() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
method.setContentChunked(true);
method.getParams().setVersion(HttpVersion.HTTP_1_0);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
fail("ProtocolException should have been thrown");
} catch (ProtocolException ex) {
// expected
} finally {
method.releaseConnection();
}
}
示例7: testEnclosedEntityNegativeLength
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public void testEnclosedEntityNegativeLength() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, -14);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
method.setContentChunked(false);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
assertEquals(200, method.getStatusCode());
String body = method.getResponseBodyAsString();
assertEquals(inputstr, body);
assertNotNull(method.getRequestHeader("Transfer-Encoding"));
assertNull(method.getRequestHeader("Content-Length"));
} finally {
method.releaseConnection();
}
}
示例8: testEnclosedEntityNegativeLengthHTTP1_0
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public void testEnclosedEntityNegativeLengthHTTP1_0() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, -14);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
method.setContentChunked(false);
method.getParams().setVersion(HttpVersion.HTTP_1_0);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
fail("ProtocolException should have been thrown");
} catch (ProtocolException ex) {
// expected
} finally {
method.releaseConnection();
}
}
示例9: createRequest
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
private void createRequest(final RequestParameters requestParameters, final PostMethod httpPost) throws IOException,
ParserConfigurationException, SAXException {
InputStream inputStream = requestParameters.getInputStream();
if (requestParameters.getContentType().contains("fastinfoset")) {
inputStream = fastInfosetCompressor.compress(inputStream);
}
if (requestParameters.getContentEncoding().contains("gzip")) {
inputStream = gzipCompressor.compress(inputStream);
httpPost.setRequestHeader("Content-Encoding", requestParameters.getContentEncoding());
}
final InputStreamRequestEntity entity = new InputStreamRequestEntity(inputStream, requestParameters.getContentType());
httpPost.setRequestEntity(entity);
httpPost.setRequestHeader("Accept", requestParameters.getAcceptHeader());
httpPost.addRequestHeader("Accept-Encoding", requestParameters.getAcceptEncoding());
if (requestParameters.getSpecialHeader() != null) {
httpPost.addRequestHeader("Special-Header", requestParameters.getSpecialHeader());
}
for (Entry<String, String> header : requestParameters.getCustomHeaders()) {
httpPost.addRequestHeader(header.getKey(), header.getValue());
}
}
示例10: createRequest
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
private void createRequest(final MultiStubRequestParameters requestParameters, final PostMethod httpPost) throws IOException,
ParserConfigurationException, SAXException {
InputStream inputStream = requestParameters.getInputStream();
if (requestParameters.getContentType().contains("fastinfoset")) {
inputStream = fastInfosetCompressor.compress(inputStream);
}
if (requestParameters.getContentEncoding().contains("gzip")) {
inputStream = gzipCompressor.compress(inputStream);
httpPost.setRequestHeader("Content-Encoding", requestParameters.getContentEncoding());
}
final InputStreamRequestEntity entity = new InputStreamRequestEntity(inputStream, requestParameters.getContentType());
httpPost.setRequestEntity(entity);
httpPost.setRequestHeader("Accept", requestParameters.getAcceptHeader());
httpPost.addRequestHeader("Accept-Encoding", requestParameters.getAcceptEncoding());
if (requestParameters.getSpecialHeader() != null) {
httpPost.addRequestHeader("Special-Header", requestParameters.getSpecialHeader());
}
httpPost.addParameter("nextstatus", requestParameters.getStatus());
httpPost.addParameter("direction", requestParameters.getDirection());
httpPost.addParameter("groupname", requestParameters.getGroupName());
}
示例11: davPut
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
public boolean davPut(File upload, String toPath) {
try {
PutMethod put = new PutMethod(getResourceUrl(toPath));
RequestEntity requestEntity = new InputStreamRequestEntity(new BufferedInputStream(
new FileInputStream(upload), HttpServices.BUFFER_SIZE), upload.length());
put.setRequestEntity(requestEntity);
/** is to allow a client that is sending a request message with a request body
* to determine if the origin server is willing to accept the request
* (based on the request headers) before the client sends the request body.
* this require server supporting HTTP/1.1 protocol.
*/
put.getParams().setBooleanParameter(
HttpMethodParams.USE_EXPECT_CONTINUE, true);
if (!executeMethod(put)) {
// handle error
System.out.println("Unable to upload file:" + toPath + " -- " + put.getStatusText());
return false;
}
return true;
} catch (Exception e) {
LOG.error(e, "Error while uploading file:" + upload.getPath());
}
return false;
}
示例12: saveStream
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
/**
* Saves a stream into a file in the repository.
*
* @param text
* Stream to be stored.
* @param url
* URL to the file that will be created.
*/
public void saveStream(InputStream stream, String url) throws IOException {
PutMethod putMethod = new PutMethod(url);
putMethod.setRequestEntity(new InputStreamRequestEntity(stream));
try {
client.executeMethod(putMethod);
putMethod.releaseConnection();
} catch (IOException e) {
throw new IOException(e);
}
finally {
stream.close();
}
}
示例13: testRetrieveTextFile
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
@Test
public void testRetrieveTextFile() throws HttpException, IOException {
DavHandler dav = new DavHandler(folders);
InputStream stream = new ByteArrayInputStream("some text".getBytes());
PutMethod putMethod = new PutMethod(
"http://localhost:9002/parent/child/textToRetrieve.txt");
putMethod.setRequestEntity(new InputStreamRequestEntity(stream));
HttpClient client = new HttpClient();
client.executeMethod(putMethod);
stream.close();
putMethod.releaseConnection();
String retrieved = dav.retrieveTextFile("http://localhost:9002/parent/child/textToRetrieve.txt");
assertEquals("some text", retrieved);
}
示例14: startLoading
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
/**
* Kicks the GDC platform to inform it that the FTP transfer is finished.
*
* @param projectId the project's ID
* @param remoteDir the remote (FTP) directory that contains the data
* @return the link that is used for polling the loading progress
* @throws GdcRestApiException
*/
public String startLoading(String projectId, String remoteDir) throws GdcRestApiException {
l.debug("Initiating data load project id=" + projectId + " remoteDir=" + remoteDir);
PostMethod pullPost = createPostMethod(getProjectMdUrl(projectId) + PULL_URI);
JSONObject pullStructure = getPullStructure(remoteDir);
InputStreamRequestEntity request = new InputStreamRequestEntity(new ByteArrayInputStream(pullStructure.toString().getBytes()));
pullPost.setRequestEntity(request);
String taskLink = null;
try {
String response = executeMethodOk(pullPost);
JSONObject responseObject = JSONObject.fromObject(response);
taskLink = responseObject.getJSONObject("pullTask").getString("uri");
} catch (HttpMethodException ex) {
throw new GdcRestApiException("Loading fails: " + ex.getMessage());
} finally {
pullPost.releaseConnection();
}
l.debug("Data load project id=" + projectId + " remoteDir=" + remoteDir + " initiated. Status is on uri=" + taskLink);
return taskLink;
}
示例15: executeMAQL
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; //導入依賴的package包/類
/**
* Executes the MAQL and creates/modifies the project's LDM
*
* @param projectId the project's ID
* @param maql String with the MAQL statements
* @return result String
* @throws GdcRestApiException
*/
public String[] executeMAQL(String projectId, String maql) throws GdcRestApiException {
l.debug("Executing MAQL projectId=" + projectId + " MAQL:\n" + maql);
PostMethod maqlPost = createPostMethod(getProjectMdUrl(projectId) + MAQL_EXEC_URI);
JSONObject maqlStructure = getMAQLExecStructure(maql);
InputStreamRequestEntity request = new InputStreamRequestEntity(new ByteArrayInputStream(
maqlStructure.toString().getBytes()));
maqlPost.setRequestEntity(request);
String result = null;
try {
String response = executeMethodOk(maqlPost);
JSONObject responseObject = JSONObject.fromObject(response);
JSONArray uris = responseObject.getJSONArray("uris");
return (String[]) uris.toArray(new String[]{""});
} catch (HttpMethodException ex) {
l.debug("MAQL execution: ", ex);
throw new GdcRestApiException("MAQL execution: " + ex.getMessage(), ex);
} finally {
maqlPost.releaseConnection();
}
}